Can someone help me with code to generate random numbers in a range of say 50-85?

This is a discussion on Can someone help me with code to generate random numbers in a range of say 50-85? within the C Programming forums, part of the General Programming Boards category; How do I write simple code to generate random numbers in a range?...

  1. #1
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Can someone help me with code to generate random numbers in a range of say 50-85?

    How do I write simple code to generate random numbers in a range?

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    #include "reallife.h"

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Just use rand() and then add the amount (50 in this case) you want the range to start at.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    n = 50 + rand() % (85-50+1);

    NOTE: rand() function is pseudo random, you may seed the rand() function using srand() function something like this

    srand(time(NULL));
    n=50+rand()%(85-50+1);

    correct me if im wrong. as it has been a while since I used this function.

    Best of Luck
    Last edited by kwigibo; 11-22-2001 at 07:04 PM.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What do you mean, "seed the rand() function"?
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The random function doesn't just pick a number out of thin air, it has to perform some math on the currently seed, and generally it then replaces it's seed with the generated number.

    So... if the user knows what the seed is, then all of a sudden your random numbers ain't so random anymore, so you need a random seed. Obviously, you can't use the rand function for this. As far as I know, there are two good ways to generate numbers 'random enough' to be seed values. One is time, and the other is temperature.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    hmmm...that makes sense.

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <limits.h>
    
    int main ()
    {
     int i;
     printf ("INT_MAX: %d\nRAND_MAX: %d\n\n", INT_MAX, RAND_MAX);
    
     printf ("--0--\n");
     srand(0);
     for (i = 0; i < 10; i++) printf ("%d ", rand());
     printf ("\n\n");
    
     printf ("--0--\n");
     srand(0);
     for (i = 0; i < 10; i++) printf ("%d ", rand());
     printf ("\n\n");
    
     
     srand(0);
     rand();
     srand (i = rand());
     printf ("--%d--\n", i);
     for (i = 0; i < 10; i++) printf ("%d ", rand());
     printf ("\n\n");
    
     srand(0);
     rand();
     srand (i = rand());
     printf ("--%d--\n", i);
     for (i = 0; i < 10; i++) printf ("%d ", rand());
     printf ("\n\n");
    
     return 0;
    }
    Network issues prevent me from actually compiling this code, so feel free to add semicolons and marks of parenthesis as needed.

    The point of using time to seed rand is so that the same string of random numbers cannot be generated in two different runs of the program.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Another thing about 'seeding' srand()

    If you make multiple calls on the rand, don't seed with the same seed again. Better yet do it once on init. As the numbers are a list and PC's are so fast it may be you get the same series of 'random' numbers if you use the time.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    /*more appropriate */
    num = (rand() %35) + 50;
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    that max's at 84 not 85 bigtamscot.....

    correctly is

    num = (rand() %36) + 50;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  2. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 09:01 AM
  3. My code wont generate randon numbers
    By n00b_101 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 05:39 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 12:59 PM

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