Thread: x=rand();

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    x=rand();

    i want to use rand function for finding random number with 4 digits how can i do that
    im only knowing how to find the maximum number which is x=rand()%10000 but how can i tell it for finding minimum number 1000

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main (void)
       {  int x;    
           int y;
    
           srand(Time(0));  // seed the random number generator.
           
           for( y = 0; y <  100; y++)
             {  
    
                x = (rand() % 9000) + 1000;    // get random number in range 1000 - 9999
    
                printf("%d\t", x); }
    
         return 0; }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    is any other combination for this? x = (rand() % 9000) + 1000;
    or is the only way i can do it

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are plenty of ways to do it. One could even say that there are an infinite number of ways to do it.

    Here's it done using the rejection method, which is not biased compared to the above:
    Code:
    int x;
    do {
        x = rand();
    } while (x >= (RAND_MAX / 9000) * 9000);
    x = x % 9000 + 1000;
    As you can see it takes much more code so you may be perfectly happy with the first method.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ddimit02 View Post
    is any other combination for this? x = (rand() % 9000) + 1000;
    or is the only way i can do it
    This is programming... there is *always* more than one solution.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by ddimit02 View Post
    is any other combination for this? x = (rand() % 9000) + 1000;
    or is the only way i can do it
    The % is the modulo operator. So it assigns x to the remainder of the output of rand() divided by 9000, plus 1000. rand() % 9000 will give you a random number between 0 and 9000. Adding 1000 to that ensures that the random number is between 10,000 and 1000.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() and seeds
    By crepincdotcom in forum C Programming
    Replies: 3
    Last Post: 01-04-2005, 07:53 PM