Random int giving only 0

H

Huub

Hi,

I have a problem using rand().

I've been trying to use it in 4 different ways: rand(), rand() % n, rand
() % n-1 and rand() % (high - low + 1) + low with high = 22 and low = 1.
Since rand() is pseudo-random, the results should show a returning
pattern. However, though it works fine in a little rand() test program
giving up to 8 different outcomes, in my real code using either way, the
only outcome I get is 0. Any idea about the cause of this?

Thanks
 
J

Jorgen Grahn

Hi,

I have a problem using rand().

I've been trying to use it in 4 different ways: rand(), rand() % n, rand
() % n-1 and rand() % (high - low + 1) + low with high = 22 and low = 1.
Since rand() is pseudo-random, the results should show a returning
pattern. However, though it works fine in a little rand() test program
giving up to 8 different outcomes, in my real code using either way, the
only outcome I get is 0. Any idea about the cause of this?

I'd say you are either mistaken (rand() returns other numbers, but you
lose them in your manipulations) or you have defined your own rand()
which is the one that really gets called.

/Jorgen
 
G

gwowen

I'd say you are either mistaken (rand() returns other numbers, but you
lose them in your manipulations) or you have defined your own rand()
which is the one that really gets called.

Or, I guess, its possible that you've misdeclared rand() somewhere,
and the compiler is only ever looking at the most significant byte(s)
of the result, which always gives zero as RAND_MAX is relatively
small, compared to the width of an integer. That's pretty unlikely
though.
 
M

Michael Doubez

I have a problem using rand().

I've been trying to use it in 4 different ways: rand(), rand() % n,  rand
() % n-1 and rand() % (high - low + 1) + low with high = 22 and low =1.

Prefer using a linear projection from [0;RAND_MAX] into you range.
Since rand() is pseudo-random, the results should show a returning
pattern.

Returning a pattern ? What do you mean ?
The output is farly random and the cyclicity is quite high for most
usual purpose.
However, though it works fine in a little rand() test program
giving up to 8 different outcomes, in my real code using either way, the
only outcome I get is 0. Any idea about the cause of this?

Wouldn't you use srand() somewhere. It is quite a common mistake to
set the seed before each use of rand().
 
H

Huub

I'd say you are either mistaken (rand() returns other numbers, but you
lose them in your manipulations) or you have defined your own rand()
which is the one that really gets called.

/Jorgen

Ok, found out it does work fine but indeed losing them somehow.
 
H

Huub

I'd say you are either mistaken (rand() returns other numbers, but you
lose them in your manipulations) or you have defined your own rand()
which is the one that really gets called.

/Jorgen

OK, can't get it working. This is the code:

in class Robot:

int Robot::Xpos(int& x)
{
x = rand(); //gives e.g.: x = 22
return 0
}


in main(): (with Robot = robot)

x_pos = robot.Xpos(x) // gives 0

What am I doing wrong here that the value of 22 isn't passed from Xpos ?\

Thank you.
 
H

Huub

Please post complete code that illustrates the problem. This code
doesn't declare x or x_pos, nor does it test their values, so it's
difficult to say what's happening. But one thing is clear: the value of
x_pos after this call will be 0, because that's what Robot::Xpos
returns.

Ok, thank you for noticing the 0. Guess I was a bit blind. Replaced it
and working ok now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top