Using rand()?

This is a discussion on Using rand()? within the C Programming forums, part of the General Programming Boards category; Is there a simple way when using the rand() function to limit it to lets say between 0 and 100?...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    25

    Using rand()?

    Is there a simple way when using the rand() function to limit it to lets say between 0 and 100?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      int number;
      number = rand() % 100;
    
      printf("Random number 0 - 100: %d", number);
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    25

    Thank You

    Just what I was looking for

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    You would have to do rand() % 101 for it to be 0-100, because if a number is some multiple of 100 then it would be modulis 0, not 100.

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, 06:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02: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

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