Like Tree1Likes
  • 1 Post By Salem

what is the range of rand()

This is a discussion on what is the range of rand() within the C Programming forums, part of the General Programming Boards category; 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 ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    stdlib.h should have something like
    #define RAND_MAX 2147483647

    Every ANSI/ISO system should have this, and it should be at least 32767
    whiteflags likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    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%.

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, 06:32 AM
  3. range?
    By tu_user in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2004, 10:35 PM
  4. Range
    By volk in forum C Programming
    Replies: 3
    Last Post: 12-19-2002, 08:43 AM
  5. Range
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2002, 10:52 AM

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