rand()

This is a discussion on rand() within the C Programming forums, part of the General Programming Boards category; Hi All What does this code mean? Code: if((rand() % xincrease) == 0) AddLetter(); (The code of the if statement) ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 12: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, 12:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 09:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21