Page 1 of 1
Random number generator
Posted: Wed Mar 12, 2008 10:10 pm
by dvwagner
Does anybody know how to generate a random number in Flowcode?
Re: Random number generator
Posted: Fri Mar 14, 2008 12:17 pm
by Steve
Re: Random number generator
Posted: Fri Mar 14, 2008 7:08 pm
by dvwagner
I don't think I know enough C to make sense of the example. Is there more of the code that is not in the example?
Re: Random number generator
Posted: Fri Mar 14, 2008 10:20 pm
by Steve
Yes - it's not quite complete.
First, follow the instructions in the 2nd post in that topic. (i.e. "#include <rand.h>" into supplementary code window and "rand.pic16.lib" into the linker parameters).
This exposes 2 C functions - "srand" (the pseudo random number seed) and "rand" (the function that actually creates the pseudo-random number).
In your Flowcode program, you would begin your routine with a C-icon containing something like this:
And to set a Flowcode variable "MyVar" to a random number, use a C-icon with this code:
Note that this program will not simulate because Flowcode cannot simulate C code. You will only see the random numbers when running on the PICmicro.
Re: Random number generator
Posted: Fri Mar 14, 2008 10:35 pm
by Steve
Another way to generate a random number is shown in the attached program.
While the user holds down a switch, a counter is incremented. When the switch is released, the random number is displayed. The "rnd = rnd MOD 100" makes sure the random number stays between 0 and 100.
Re: Random number generator
Posted: Fri Jul 16, 2010 10:24 am
by wtulp
Thanks
It works very fine now.