Random Number

pottsy1000000

New Member
Hi everyone,

I'm picking up a project that I've not looked at since last year. I had some great advice from you last year on how to generate a random number between 2 fixed points. However the Set to Random command seems to always set the initial variable to '2' and so the formula that follows always comes to the same number between the fixed point. I'm using the Blockley and can't see any other way to use the Set to Random command. Has anyone had the same problem before? and what can you recommend?

I'm not sure if it helps, but here is a screen shot of the code attached.

Many thanks in advance
 

Attachments

AllyCat

Senior Member
Hi,

Every computer "random" algorithm needs a random "seed" to give different results from a "cold start". For more sophisticated computers it can be the current date or time (which is also possible with a fresh PE6 download), but you may need to be more creative with a PICaxe. Possibilities are a value for the temperature, battery voltage, light level or just the variable from a "touch16" command (for a pin with little or nothing connected).

I don't know about blockly, but hippy's post #9 in this recent thread looks like a good start.

Cheers, Alan.
 

pottsy1000000

New Member
Hi,

Every computer "random" algorithm needs a random "seed" to give different results from a "cold start". For more sophisticated computers it can be the current date or time (which is also possible with a fresh PE6 download), but you may need to be more creative with a PICaxe. Possibilities are a value for the temperature, battery voltage, light level or just the variable from a "touch16" command (for a pin with little or nothing connected).

I don't know about blockly, but hippy's post #9 in this recent thread looks like a good start.

Cheers, Alan.
Hi Alan, thanks for taking the time to read and reply. I've had a look at the thread that you suggested. It looks like the key difference is that the suggested code is looping continually, which presumably allows the seed you mentioned to grow.

However, I don't want a continuous loop, I just need one random number at the start of the program. So I tried to put a limited loop in to keep generating a random number . I used the Repeat Until command and incremented a new variable on each loop until I had reached the limit I set. I experimented with a few increasing number of loops, but I found that on each run, the sequence of random numbers was exactly the same and therefore the final value of the random number was always the same.

Is there anything else you can suggest for me please?
 

AllyCat

Senior Member
Hi,

the final value of the random number was always the same.
Yes, computer programs are "deterministic". which means that they always do exactly the same thing - if they didn't do that, they normally wouldn't be very useful ! To provide a "random" element, you need to apply some "external influence" into the program - I gave some examples how that might be done in my previous post.

But needing only one "random" number is rather unusual - you need to tell us what is its purpose, and what you mean by "number" - a single decimal digit (0-9), a byte (0-255), a word (0 - 65535) or a Premium Bond number ? Something like a light level into an ADC input (as suggested in the other thread) may be good enough as a seed, but not as a random number itself. There will probably be a high preponderance of "low" values (low light levels), gradually reducing at higher light levels, but perhaps then an enormous number of the "maximum" value (analogue overload).

Probably the easiest way to get a reasonably good random number is to start the program "counting" as fast as possible (or better, looping the random function) when power is applied and then wait for the "user" to press a button after some seconds (or minutes).

Cheers, Alan.
 

hippy

Technical Support
Staff member
I don't want a continuous loop, I just need one random number at the start of the program.
One trick is to keep the last random number in EEPROM. Read that at the start of the program, generate the next random number from that last, store the new random number in EEPROM ready for the next restart.
 

AllyCat

Senior Member
Hi,

Ah yes, a very good idea. But if you ever need to revise (reload) the program, don't forget to include a #NO_DATA command at the top, or to change any seed written to the EPROM.

Cheers, Alan.
 

fernando_g

Senior Member
A good random word seed is an ADC read from a microphone amplifier. If you make 2 or more (up o 64) consecutive readings and add them together, it will be even better.
 

pottsy1000000

New Member
you need to tell us what is its purpose, and what you mean by "number" - a single decimal digit (0-9), a byte (0-255), a word (0 - 65535) or a Premium Bond number ?
Hi Alan,

Thanks. Some background then. Its a Music Box type project. There are a number of tunes preloaded to an SD Card. The trigger is a micro switch connected to the lid of the box, so when that's activated it powers up the device and starts to run the code. The idea is that it plays one track at random whilst the lid is lifted.

In December, Hippy gave me some good code ideas to generate the number between the boundaries of the Tracks that are on the SD card e.g. 1 to 20, but I have always been stuck on how to generate a different track number. And it seems to stem from that initial random number generation.

Here is the original thread to see if it helps; http://www.picaxeforum.co.uk/showthread.php?29508-How-to-generate-a-random-number-between-fixed-points

Do you think there is a different way to do it?

Thanks
 

AllyCat

Senior Member
Hi,

So it doesn't need to be "seriously" random (as for a lottery draw for example). In fact, as hippy has suggested before, you may not even want something truly random. With 20 tracks, and a truly random generator, there is a 5% chance that it will play the same track twice consecutively and a 0.25% chance it will play it three times !

You might even want to consider something more like a bingo caller or a card deal. There, once a number/card has been called/dealt, then it cannot appear again until the game/pack has been "reset". In the extreme case you might play ALL the tunes in a random order, but not more than once. That needs a very different program to a random number generator.

For your application, the simplest solution may be to generate a convenient random number range that is larger than the number of tracks, for example 0 - 31 (5 bits) if there were 20 tracks. Then the program can "reject" any unsuitable values, e.g. > 20, and perhaps the last track played, and simply "try again", which may actually help the randomising procedure. Hippy's EPROM method is a very good idea, but for a really "good" random generator you should execute the RANDOM function at least as many times as there are bits in the value you're using (i.e. 5 times for my 0-31 example above), because of the way the function works.

Cheers, Alan.
 
Top