Page 1 of 1

Random LED's

Posted: Sat Apr 13, 2013 10:28 am
by alek0161
Dear fellow Flowcode users

My wish is to create a small program which can generate a random variable and then make a random led turn on.
So far I have two LEDarrays with 3 leds in each and I want one LED to be turned on in each LEDarray, the LED should be selected by the random variable. As I do not want the same LED in each LEDarray to turn on, I have made two different random variables. BUT the two random variables generate the same random value each time.
My quenstion is: How can I make different variables generate different values in the same program?

I really hope anybody is able to help. If there is any quenstions please ask me.

Best regards
Aleksander Rosenkilde Kristiansen

P.S. I have attached my program.

Re: Random LED's

Posted: Sat Apr 13, 2013 9:10 pm
by JonnyW
Hello Aleksander.

Flowcode has a random number function, random(), built in. This should return a different value each time you call it.

If this is not what you are after or are using (sorry - I have Linux at home so can not see your program) Here is some pseudo-code for a simple random 16-bit number. Note the 'randval' global variable is not initialised at the start. This helps with a different random number each time.

Code: Select all

randval = randval + 0xCA56
randval = (randval >> 3) + (randval << 13)
.Return = randval
You can call this constantly in a timer interrupt if you want, or each time you need it. Then, if you want a value between 0 and N do:

Code: Select all

value = GetRandom()
value = value MOD N
It may also help to look at Martins lottery generator, here:
http://www.matrixmultimedia.com/mmforum ... 885#p22091

Good luck,

Jonny