Random() function

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Random() function

Post by Mark »

I am trying to use the random() function.

"R = random()" compiles OK to C but then when compiling to assembler gives:

"
.......................................

Nokia LCD Plot 2.c(874:10): error: unknown identifier 'rand'
Nokia LCD Plot 2.c(874:10): error: failed to generate expression
Nokia LCD Plot 2.c(874:10): error: invalid operand 'rand()'
Nokia LCD Plot 2.c(874:8): error: failed to generate expression
Nokia LCD Plot 2.c success

failure
"

R is defined as int, chip 16F887, and removing the above line removes the problem.

There is nothing special in the code otherwise. Sorry I cannot see anything in the help file and adding a value e.g. random(1) does not seem allowed? Advice please.

Thanks

ps saw

If using random() function:
#include <rand.h> should be placed in Supplementary code window. ('View' 'Project Options' & click on 'Use Supplementary code')

If this is the answer then is there a fix in v4.5?
Go with the Flow.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Random() function

Post by Sean »

We will look into this problem.

A quick fix is to add the following line to the supplementary code section of Flowcode (View -> Project Options menu):

#include <rand.h>

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random() function

Post by medelec35 »

For anyone interested in using the random() function to generate random numbers, I have posted a lottery generator:
http://www.matrixmultimedia.com/mmforum ... 967#p22091
Target can be easily changed. If there is a compiling error with tmr0l, then your target has an 8 bit timer 0 so just remove l at the end and recompile.

Random() is not really that random since same number sequences are always used from start up.
The the lottery generator flowchart gets round this (hopefully :P )

Hope this helps

Martin
Martin

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Random() function

Post by Dave S »

medelec35 wrote:Random() is not really that random since same number sequences are always used from start up.
I used the seed srand(tmr0);

tmr0 is "unknown" on power up. Seems to work quite well.

Dave.

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

Re: Random() function

Post by Mark »

Dave,

Thanks, I understand the concept, but how would I actually impliment that in Flowcode?

In a C icon? If so please post the syntax.

Thanks (and to Martin also)
Go with the Flow.

Dave S
Posts: 39
Joined: Tue Jun 07, 2011 12:37 pm
Has thanked: 12 times
Been thanked: 5 times
Contact:

Re: Random() function

Post by Dave S »

Mark wrote:Dave,

Thanks, I understand the concept, but how would I actually impliment that in Flowcode?

In a C icon? If so please post the syntax.

Thanks (and to Martin also)
I just put srand(tmr0); in a C box at the start of the flowchart, then RandomNumber = random() in a calculation box when a random number is required.
RandomNumber is a variable, call it what you like.
I found the number sequence was different each time I powered up.

Dave.

Post Reply