Page 1 of 1

Random() function

Posted: Tue Sep 20, 2011 9:50 pm
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?

Re: Random() function

Posted: Tue Sep 20, 2011 10:06 pm
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>

Re: Random() function

Posted: Wed Sep 21, 2011 7:27 am
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

Re: Random() function

Posted: Fri Sep 23, 2011 10:28 am
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.

Re: Random() function

Posted: Fri Sep 23, 2011 6:32 pm
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)

Re: Random() function

Posted: Fri Sep 23, 2011 8:50 pm
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.