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?
Random() function
Moderator: Benj
-
Sean
- Valued Contributor

- Posts: 548
- Joined: Tue Jun 26, 2007 11:23 am
- Has thanked: 6 times
- Been thanked: 44 times
Re: Random() function
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>
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
Re: Random() function
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
)
Hope this helps
Martin
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
Hope this helps
Martin
Martin
Re: Random() function
I used the seed srand(tmr0);medelec35 wrote:Random() is not really that random since same number sequences are always used from start up.
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
Re: Random() function
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)
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.
Re: Random() function
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.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)
RandomNumber is a variable, call it what you like.
I found the number sequence was different each time I powered up.
Dave.