Thread: rand()

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    rand()

    Hi All

    What does this code mean?

    Code:
    if((rand() % xincrease) == 0)
    AddLetter();
    (The code of the if statement)

    Thanx

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    rand() returns pseudorandom value from 0 to RAND_MAX inclusive

    a % b gives reminder for division a to b

    if reminder is zero - it means a == b * m for some m
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    Will increasing xincrease (ie, 'b') increase the probability of the statement being true?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    >Will increasing xincrease (ie, 'b') increase the probability of the statement being true?

    No, decreasing it will increase the probability of the statement being true.

    rand() % 1 == 0, true for every value from rand()
    rand() % 2 == 0, true for every 2nd value from rand()
    rand() % 3 == 0, true for every 3rd value from rand()
    rand() % n == 0, true for every n:th value from rand()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM