# Random problems - Dice.

I'm working on an Arduino based dice, with just 6 LEDs. I'm generating a random number and the number should show as LEDs burning. But the random function in Arduino is not really random, it repeats itself. I've been looking into it and it seems that the solution isn't as easy as I thought. Is there any way I can make my random number real (or just a bit more) random? Any solution?

code so far:
http://kunststofarchipel.nl/dobbelsteen_twee.ino

I'm on an Arduino Uno and yes, very novice.

Please let me know!

Kind regards,

Thomas.

Random numbers will repeat. There has been a discussion on this recently. Try the search function at the top right of the screen to find more information.

try use randomseed on an unconnected analog input pin as per:

Look up Mersenne Twister. (I'm not sure of spelling). You have room for at least a couple of hundred bytes of state.

You could also do the random in hardware, for example buying a geiger counter kit, timing the microseconds between clicks, and time%6 will be pretty random but needs testing thoroughly.

The key property of a source of randomness is that the output is
completely unpredictable (statistically), not that it looks random
to a human.

For a dice throw there's a 1/6 chance on each throw that its a repeat
of the last, just as there's 1/6 chance of any outcome. If the chance
of repeat were less than 1/6 then something is fishy with your dice!

ad2049q:
Look up Mersenne Twister. (I'm not sure of spelling). You have room for at least a couple of hundred bytes of state.

You could also do the random in hardware, for example buying a geiger counter kit, timing the microseconds between clicks, and time%6 will be pretty random but needs testing thoroughly.

But don't time it in milliseconds, millis() is rather biased as sometimes it jumps
2 steps. micros() probably has some slight bias too (look how its coded).

A better approach is to look at each pair of timings and generate a 0 if the first
is longer, a 1 if the second is longer. Reverse the sense each time. This is the easiest
way to generate unbiased bits from a source of randomly timed events. Hotbits uses
it I believe. HotBits: Genuine Random Numbers

TRex:
try use randomseed on an unconnected analog input pin as per:
randomSeed() - Arduino Reference

I've been looking into this but I can't seem to apply this to my script. The problem is with the 'printIn' part. Any idea on how might work?

Updated script: http://kunststofarchipel.nl/dice_two.ino

Ah, the random seed seems to work. It now shows (a kind of!) random number.

Thanks!

You don't need the Serial.println.

Here's your code, rewritten to use loops, and the randomSeed:

``````byte pins[] = { 3, 5, 7, 9, 11, 13 };

void setup()
{
randomSeed(analogRead(A0));
for (byte i = 0; i < 6; i++) {
pinMode(pins[i], OUTPUT);
}
}

void flits(byte waarde) {
for (byte i = 1; i <= 6; i++) {
for (byte j = 0; j < waarde; j++) {
digitalWrite(pins[j], i % 2);
}
delay(100);
if (i < 6) delay(900);
}
}

void loop()
{
byte waarde = random(1,7);

flits(waarde);
}
``````

i wouldn't it be simple to just loop trough 1-6 as fast as possible, and stay on the one that you currently have when the user presses the button? the user pressing the button is one of the most random things you have, and is so easily used for this. and when looping full speed, there is no way for the user to manipulate the result by careful timing.

racemaniac:
i wouldn't it be simple to just loop trough 1-6 as fast as possible, and stay on the one that you currently have when the user presses the button? the user pressing the button is one of the most random things you have, and is so easily used for this. and when looping full speed, there is no way for the user to manipulate the result by careful timing.

Unlike, for instance, a roulette wheel... See a book called "The Newtonian Casino" for
details

Very nice post, topic discussion is really good. It wouldn't be easy for user to stay there when press the button but easy to do random things.