View unanswered posts | View active topics It is currently Wed Aug 20, 2014 2:32 pm






Reply to topic  [ 9 posts ] 
random is not working properly 
Author Message
Rookie

Joined: Wed Mar 28, 2007 9:15 am
Posts: 5
Post 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
Profile
Rookie

Joined: Fri Mar 09, 2007 12:02 am
Posts: 11
Post 
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
Profile YIM
Rookie

Joined: Wed Mar 28, 2007 9:15 am
Posts: 5
Post 
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
Profile
Rookie

Joined: Fri Mar 09, 2007 12:02 am
Posts: 11
Post 
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
Profile YIM
Rookie

Joined: Wed Mar 28, 2007 9:15 am
Posts: 5
Post 
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
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
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
Profile WWW
Rookie

Joined: Wed Mar 28, 2007 9:15 am
Posts: 5
Post 
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
Profile
Rookie

Joined: Wed Mar 28, 2007 9:15 am
Posts: 5
Post Re: random is not working properly
ROBOWhizC wrote:
Also, this code:
unsigned int rand;
rand = random(10000);
rand = rand*4;

sometimes returns a negative number for rand.


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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
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 8) 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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.