can anyone explain me the usage of random function

what does this line mean?

radom() %10;

Printable View

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

what does this line mean?

radom() %10; - 07-17-2002Salem
Perhaps

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

Will get the message across if you run it a few times - 07-17-2002Davros
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-18-2002onurak
thx

- 07-18-2002civix
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-2002Hammer
>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-2002raimo
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-2002borko_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-2002civix
SINCE DEV-C++ WAS CREATED, HAMMER!!!

THOSE ARE THE GUIDELINES FOR DEV-C++ - 07-18-2002Govtcheez
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-2002raimoQuote:

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

THOSE ARE THE GUIDELINES FOR DEV-C++

- 07-18-2002HammerQuote:

*Originally posted by civix*

**SINCE DEV-C++ WAS CREATED, HAMMER!!!**

THOSE ARE THE GUIDELINES FOR DEV-C++

- You stated this

Quote:

one million is the highest number that you can put behind the %

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-2002Govtcheez
> 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-2002ygfperson
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-2002civix
ohhhhh ok....