Author |
Message |
ROBOWhizC
Rookie
Joined: Wed Mar 28, 2007 9:15 am Posts: 5
|
 random is not working properly
I have been trying to use the random(range) function, and it works when a value is put in for range (for example: random(10000)).
But when it's given a variable it only returns a 1 or a 0.
int total = 10000; int rand; rand = random(total);
rand is only a 1 or 0. Any ideas why this might be?
Also, this code:
unsigned int rand; rand = random(10000); rand = rand*4;
sometimes returns a negative number for rand.
|
Wed Mar 28, 2007 9:30 am |
|
 |
sessyargc
Rookie
Joined: Fri Mar 09, 2007 12:02 am Posts: 11
|
I haven't tried out your code yet but you might want to add an srand() call before the random() call.
srand() seeds/prepares the random() call.
if it doesn't work out post again.
|
Wed Mar 28, 2007 10:56 am |
|
 |
ROBOWhizC
Rookie
Joined: Wed Mar 28, 2007 9:15 am Posts: 5
|
What are you supposed to put as the seed in the srand(seed) ? Or should I leave it blank?
|
Wed Mar 28, 2007 12:23 pm |
|
 |
sessyargc
Rookie
Joined: Fri Mar 09, 2007 12:02 am Posts: 11
|
I'm not really sure how RobotC implements srand() and random(). Though you should call srand() with any value you want, except maybe 1. According to some online documents, "if seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to random() or srand()".
Leaving it blank would definitely result with an error as the RobotC compiler would likely expect you to pass a value to it.
goodluck!
|
Wed Mar 28, 2007 4:25 pm |
|
 |
ROBOWhizC
Rookie
Joined: Wed Mar 28, 2007 9:15 am Posts: 5
|
It's still not working... I still don't understand what srand is supposed to do and where I should put it.
|
Thu Mar 29, 2007 5:06 pm |
|
 |
starwarslegokid
Moderator
Joined: Wed Jan 31, 2007 3:39 am Posts: 299 Location: San Diego, California. USA
|
try this test code to see if its the random code is causing a problem. It appears to work for me. Run this code and it will display your random number on the NXT screen. If it continues to not work, you could try to update your NXT firmware. Let ,me know what happens.
task main()
{
while (true)
{
int total = 10000;
int rand;
rand = random(total);
nxtDisplayTextLine(3,"R=%d", rand);
wait1Msec(500);
}
}
_________________Mmmm Legos B-) My Robot Projects: http://www.freewebs.com/robotprojects/
|
Thu Mar 29, 2007 5:45 pm |
|
 |
ROBOWhizC
Rookie
Joined: Wed Mar 28, 2007 9:15 am Posts: 5
|
Yes, that code works.
The problem was when I tried to implement the random function in a much larger piece of code. I copied the exact code that used the random function from my main code and created a separate program with just that part of the code, and the random generator worked... But then in the main program, it went back to only displaying a 1 or 0.
|
Thu Mar 29, 2007 10:16 pm |
|
 |
ROBOWhizC
Rookie
Joined: Wed Mar 28, 2007 9:15 am Posts: 5
|
 Re: random is not working properly
To answer my own question for the benefit of others, defining an integer in RobotC seems to set the variable up as a short integer, which has a range of +32767 to -32767, so when the number becomes greater than 32767, it wraps around to the negative.
To fix this, I used a long integer, which has a range of +2,147,483,647 to -2,147,483,647.
|
Fri Mar 30, 2007 6:03 pm |
|
 |
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 616
|
The size of "int" (i.e 8-bit, 16-bit or 32-bit) is a platform specific parameter in the C language specificaiton. Many embedded systems use 16 (or even  bit variables for 'int'.
RobotC current defines all 'int' as 16-bit variables. This may change in the future with the smaller less-capable hardware platforms (e.g. RCX) using 16-bits and the larger more-capable platformes (e.g. NXT) using 32-bits.
|
Tue Apr 03, 2007 9:12 am |
|
|