Random number generator

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
dvwagner
Flowcode V4 User
Posts: 8
Joined: Tue Mar 04, 2008 11:24 pm
Contact:

Random number generator

Post by dvwagner »

Does anybody know how to generate a random number in Flowcode?

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Random number generator

Post by Steve »


dvwagner
Flowcode V4 User
Posts: 8
Joined: Tue Mar 04, 2008 11:24 pm
Contact:

Re: Random number generator

Post 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?

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Random number generator

Post 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:

Code: Select all

srand(100);
And to set a Flowcode variable "MyVar" to a random number, use a C-icon with this code:

Code: Select all

FCV_MYVAR = rand();
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.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Random number generator

Post 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.
Attachments
rnd.fcf
(5 KiB) Downloaded 1585 times

wtulp
Flowcode V4 User
Posts: 10
Joined: Mon Jan 11, 2010 12:59 pm
Contact:

Re: Random number generator

Post by wtulp »

Thanks

It works very fine now.

Post Reply