Thread: random number within user entered range

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    random number within user entered range

    Code so far:


    Code:
    #include <iostream>
    #include <cstdlib> 
    using namespace std;
    
    int main(void){
    
    int a, b, c,d,n, guess;
    
    cout << "enter a range of number to guess from.\n";
    
    cin >> a >> b;
    
    d = rand() ; // need to make this within range entered by user
    
    cout << d;
    
    cout << "how many guesses do you want?\n";
    
    cin >> c;
    
    	for (int i=0; i<c; i++){
    		cout << "Enter a guess:\n";
    		cin >> guess;
    
    		if(guess == d){
    			cout << "You got it!\n";
    			return 0;
    		}
    		else if(guess < d)
    			cout << "too low\n";
    		else 
    			cout << "too high\n";
    
    	}
    }
    I need the random number to be within the range the user enters. I don't get how. I read about rand() %10 is 0-9.. but how do i use the users input to create a random number within their range.

    thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I guess you could use rand()%(b-a) + a.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The general use is:
    Code:
     rand() % (HIGH-LOW+1)+LOW
    Here it'll be:
    Code:
     rand() % (b-a+1)+a
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Are you even getting random numbers without seeding it?

    to seed the radndom numbers you declare srand(time(NULL)); below your int's.

    also you'll have to include <ctime> in your header files.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This is likely to produce a "more random" value. (It depends on the implementation of the `rand' function.)

    Soma

    Code:
    int random(int min, int max)
    {
    	using namespace std;
    	return(min + ((rand() * (1.0 / (RAND_MAX))) * (max - min)));
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    242
    as nick mentioned, you really should seed it. here, if you're lucky, the user's entries may not make it immediately apparent, but rand() without seeding cranks out the same sequence of "random" numbers every time.

    with seeding, i think sipher's formula is "random enough" for most purposes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-08-2010, 07:15 PM
  2. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 10:12 AM
  3. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  4. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM