Random number generator
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
Re: Random number generator
This thread may help:
http://www.matrixmultimedia.com/mmforum ... ndom#p7442
http://www.matrixmultimedia.com/mmforum ... ndom#p7442
Re: Random number generator
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?
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
Re: Random number generator
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.
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:
Code: Select all
srand(100);
Code: Select all
FCV_MYVAR = rand();
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
Re: Random number generator
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.
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.
- Attachments
-
- rnd.fcf
- (5 KiB) Downloaded 1636 times