Thread: what is the range of rand()

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    what is the range of rand()

    Does rand() not generate numbers between 0 to 32,767.
    source: http://mathbits.com/mathbits/compsci...yFunc/rand.htm

    HOwever, when I run the below code, I'm getting huge numbers like:

    717154347, 81702301

    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      srand((unsigned) time(NULL));
      printf("number is: %d", rand());
    
      return 0;
    }

    example output

    http://codepad.org/cwrqFEFf
    Last edited by bos1234; 06-03-2012 at 07:45 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It produces numbers in range of its return type, in this case, int as prototyped.

    The int type is defined by the standard to be at least two bytes, which gives the signed range of a short, or -32768 through 32768. However, on most modern computers, it's four bytes, giving an exponentially larger range.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    stdlib.h should have something like
    #define RAND_MAX 2147483647

    Every ANSI/ISO system should have this, and it should be at least 32767
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you want values between 0 and 32767, one technique is to compute rand()%32767 to obtain the lower order bits of the value returned by rand(). Another possible approach is to compute rand()/(RAND_MAX/32767 + 1) - which extracts the higher order bits of the result returned by rand(). There are other possible approaches as well.

    Note, however, that such approaches can destroy the randomness of the resultant values - depending on how rand() is actually implemented. If you care about how random the values actually are, then test to be sure.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using rand() to create a range of numbers
    By keira in forum C Programming
    Replies: 7
    Last Post: 09-11-2007, 01:45 AM
  2. Difference in Unseeded rand() and seeded rand()
    By relientk_man in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2005, 07:32 AM
  3. range?
    By tu_user in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2004, 11:35 PM
  4. Range
    By volk in forum C Programming
    Replies: 3
    Last Post: 12-19-2002, 09:43 AM
  5. Range
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2002, 10:52 AM