can anyone explain me the usage of random function
what does this line mean?
radom() %10;
can anyone explain me the usage of random function
what does this line mean?
radom() %10;
C++ Makes you Feel Better
"Gravity connot be held reponsible for people falling in love"--Albert Einstein
Perhaps
cout << random() % 10 << endl;
Will get the message across if you run it a few times
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.
thx
C++ Makes you Feel Better
"Gravity connot be held reponsible for people falling in love"--Albert Einstein
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
.
>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?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.
I am not using Dev-C++.
#!/usr/bin/env python
import sys;file=open(sys.argv[0]);print file.read();file.close()
% 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
SINCE DEV-C++ WAS CREATED, HAMMER!!!
THOSE ARE THE GUIDELINES FOR DEV-C++
.
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...
-Govtcheez
[email protected]
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...>the %10 means that the output will be between 0 and 10..
THOSE ARE THE GUIDELINES FOR DEV-C++
I am not using Dev-C++.
#!/usr/bin/env python
import sys;file=open(sys.argv[0]);print file.read();file.close()
- no need to shout, I can hear you loud and clear.Originally posted by civix
SINCE DEV-C++ WAS CREATED, HAMMER!!!
THOSE ARE THE GUIDELINES FOR DEV-C++
- You stated this
... 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().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?!
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
> 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
-Govtcheez
[email protected]
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.