Thread: random()?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    106

    random()?

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps

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

    Will get the message across if you run it a few times

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    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.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    thx
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    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
    .

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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]

  7. #7
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    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()

  8. #8
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    % 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

  9. #9
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    SINCE DEV-C++ WAS CREATED, HAMMER!!!
    THOSE ARE THE GUIDELINES FOR DEV-C++
    .

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    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...

  11. #11
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    >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...
    I am not using Dev-C++.
    #!/usr/bin/env python
    import sys;file=open(sys.argv[0]);print file.read();file.close()

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    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?!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 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

  14. #14
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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.

  15. #15
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    ohhhhh ok....
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM