# random()?

• 07-16-2002
onurak
random()?
can anyone explain me the usage of random function

what does this line mean?

• 07-16-2002
Salem
Perhaps

cout << random() % 10 << endl;

Will get the message across if you run it a few times
• 07-17-2002
Davros
random() returns a value between 0 & MAX_RAND (usually 2^31).

Because % returns the reminder of a division, calling random() % 10 will return a number between 0 & 9 inclusive.
• 07-17-2002
onurak
thx
• 07-18-2002
civix
the %10 means that the output will be between 0 and 10..if it was %20, that would make a random number between 0 and 20...one million is the highest number that you can put behind the % (dont you just hate counting 0's?)...hope that helps :)
• 07-18-2002
Hammer
>the %10 means that the output will be between 0 and 10..
Wrong, it's 0-9, as already stated.

>one million is the highest number that you can put behind the %
Since when?
• 07-18-2002
raimo
You should not use modulus operator before you know what it is.

p / q = s + r/q

p,q,r,s are integers. r < q

Then also: p % q = r

% 10 means that you take the last number of an integer.

Therefore the values are not completely equally probable. This matters when you use a very large divisor.
• 07-18-2002
borko_b
% is used for getting the remainder of a division...

5 / 2 = 2 <-quotient
5% 2 = 1 <- remainder 5 = 2*2 +1

10/3 = 3
10%3=1 -> 10 = 3*3 +1

64/8=8
64%8=0 -> 64= 8*8 + 0
• 07-18-2002
civix
SINCE DEV-C++ WAS CREATED, HAMMER!!!
THOSE ARE THE GUIDELINES FOR DEV-C++
• 07-18-2002
Govtcheez
Whoops...

My original post wasn't totally accurate. The highest random value Dev-c++ 4 can produce is 32767, not a million, civix. Still, that's totally compiler dependant. It could be something totally different on Dev-c++ 5...
• 07-18-2002
raimo
Quote:

>the %10 means that the output will be between 0 and 10..

THOSE ARE THE GUIDELINES FOR DEV-C++
Your compiler can return 10 when taking mod 10? Would you tell me that for which x does this return 10: x%10 ? I don't think that Dev-C++ developers where so dummy that they created compiler which returns 10 when taking modulus 10. Same like if Microsoft created a compiler which returns 3 when calculating 3-2...
• 07-18-2002
Hammer
Quote:

Originally posted by civix
SINCE DEV-C++ WAS CREATED, HAMMER!!!
THOSE ARE THE GUIDELINES FOR DEV-C++

- no need to shout, I can hear you loud and clear.
- You stated this
Quote:

one million is the highest number that you can put behind the %
... and I took that to be regarding the modulos operator (ie nothing to do with random number functions). Even if you were talking about the random() function, you're still wrong, as the number following % has nothing to do with the return from random().

Eg if random() returned 32,000 and the calculation might be
>32,000 % 1,000,001L
which is perfectly valid, as far as I know, although the result will of course be 32000. Maybe you can put me right and prove it isn't valid?!
• 07-18-2002
Govtcheez
> Maybe you can put me right and prove it isn't valid?!

No, you're right... it'll compile and run fine, but the value won't be over 32767 on Dev-c++ 4
• 07-18-2002
ygfperson
to get remainders in assembly code you divide a 64-bit number by a 32-bit number and get a 32-bit remainder and a 32-bit devisor. one million isn't even a power of 2. dev-c++ may have that limitation for some reason, but theoretically it should work fine with values in a normal integer's range.
• 07-18-2002
civix
ohhhhh ok....