Arduino Forum

Using Arduino => Audio => Topic started by: bddbddbdd on Mar 30, 2016, 12:04 am

Title: random player (random at reset)
Post by: bddbddbdd on Mar 30, 2016, 12:04 am
Hi there -
I searched for "random players" and found more than a few examples - but I'm specifically interested in how one would make sure a random player *started* randomly, each time... I had some trouble finding this.

My current code is essentially the same as Michael P. Flaga's... it reads as follows. Can someone help me?

Code: [Select]

// libraries
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

// initialize MP3 card
SdFat sd;
SFEMP3Shield MP3player;
byte result;

// setup
void setup() {
  result = sd.begin(SD_SEL, SPI_FULL_SPEED);
  result = MP3player.begin();
  MP3player.setVolume(12, 12);
  randomSeed (millis());
}

// loop
void loop() {
  MP3player.playTrack(random(5)); // range of tracks to randomly play.
  while (MP3player.isPlaying());   
  MP3player.stopTrack();// stop track
}
Title: Re: random player (random at reset)
Post by: uxomm on Mar 30, 2016, 01:46 am
I do not think that
Code: [Select]
randomSeed(millis());
will produce "random" numbers in your setup.

Take a look at the reference: randomSeed (http://www.arduino.cc/en/Reference/RandomSeed)

randomSeed(millis()) can make a good "seed" if there is some interaction with a human pressing a button (not the reset button), but in your setup, millis() will always give the same number and so random will always give the same sequence.

Title: Re: random player (random at reset)
Post by: borland on Mar 30, 2016, 02:03 am
You may find a random bias toward selecting certain files.  To remove any random bias, take a look at the Arduino code on from these two blog articles....

http://www.utopiamechanicus.com/article/arduino-better-random-numbers/ (http://www.utopiamechanicus.com/article/arduino-better-random-numbers/)


http://www.utopiamechanicus.com/article/better-arduino-random-numbers/ (http://www.utopiamechanicus.com/article/better-arduino-random-numbers/)



 
Title: Re: random player (random at reset)
Post by: bddbddbdd on Mar 30, 2016, 02:19 am
Thanks folks, that does the trick!