10 Replies - 252419 Views - Last Post: 31 August 2012 - 07:27 PM Rate Topic: -----

#1 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Random number between 2 ints

Post icon  Posted 30 September 2008 - 03:08 PM

Hey all!
Kinda new to C++ (as evidenced soon).
I programmed a bit in Java, but C++ is a different monster. ;)

I was just wondering how I would go about getting a random number between 2 user defined numbers.

I have srand seeded with the time, so the number should be random, but all of the references that I look up that have rand() all use 0 - n. And not a user-defined beginning value.

Thanks in advance!

-John

Is This A Good Question/Topic? 0
  • +

Replies To: Random number between 2 ints

#2 realNoName   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Random number between 2 ints

Posted 30 September 2008 - 03:18 PM

take a look at this tutorial

http://www.dreaminco...wtopic14057.htm
Was This Post Helpful? 1
  • +
  • -

#3 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1614
  • View blog
  • Posts: 8,462
  • Joined: 07-September 06

Re: Random number between 2 ints

Posted 30 September 2008 - 03:19 PM

do something like so:
(rand()%(max-min))+min;

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#4 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Re: Random number between 2 ints

Posted 30 September 2008 - 03:21 PM

View PostrealNoName, on 30 Sep, 2008 - 03:18 PM, said:

take a look at this tutorial

http://www.dreaminco...wtopic14057.htm


I did.
That tutorial does the same - provides how to go from 0 - n.

How about a random number from int1 - int2?
Was This Post Helpful? 0
  • +
  • -

#5 realNoName   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Random number between 2 ints

Posted 30 September 2008 - 03:27 PM

read the second part

Quote

Although this is a random number, we need to do something to reduce it to a given range.

This is accomplished by applying the modulus operator (% in most languages) to the number.

Any number "modded" by 3 will either be 0, 1, or 2.

If we want a number from 0 to 9, we would write
rand() % 10;


while adding 1 would give us 1 to 10;

rand() % 10 + 1;


so all you would have to to is make a var min and max and replace 10 with max and 1 with min
rand()%(max-min)+min;

This post has been edited by realNoName: 30 September 2008 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Re: Random number between 2 ints

Posted 30 September 2008 - 03:28 PM

View PostrealNoName, on 30 Sep, 2008 - 03:18 PM, said:

take a look at this tutorial

http://www.dreaminco...wtopic14057.htm


AH!
Thank you, I'm sorry, I should've thought about that... =|

I'm still having a problem though... It sometimes generates numbers out of the bounded numbers, but as far as I know, it shouldn't.

	
	srand (time(NULL));
	randNum = (rand()%(hi-lo))+ lo;



And my full program is...

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	// Variables
	bool correctGuess = false;
	int num1, num2, lo, hi, randNum, guess;
	int numGuess = 0;
	
	// Get/Test inputs
	cout << "Please enter 2 integers." << endl;
	cin >> num1 >> num2;
	
	if (num1 > num2)
	  {
		lo = num1;
		hi = num2;
	  }
	else
	  {
		lo = num2;
		hi = num1;
	  }
	
	// Random number time
	srand (time(NULL));
	randNum = (rand()%(hi-lo))+ lo;
	
	cout << "Hit me with your best guess, sucka!" << endl;
	
	do
	{
		 guess = 0;
		 cin >> guess;
		 numGuess ++;
		 
		 if (guess != randNum)
			{
				   if (guess > randNum)
				   {
					  cout << "You were too high! Bring it down, son!"
						   << endl;
				   }
				   else
				   {
					  cout << "You were too low! Get yo chin up!"
						   << endl;
				   }
			}
		 else
			{
				 cout << "You finally got it!" << endl
					  << "I guess even the blind squirrel can find"
					  << " the nut sometimes!" << endl
					  << "You took " << numGuess;
					  
					  if (numGuess > 1)
						  {cout << " guesses.";}
					  else
						  {cout << " guess.";}
				 correctGuess = true;
			}
	}while (!correctGuess);
	
	cin.get();
	cin.get();
	return 0;
}



Thanks for all your help!
Was This Post Helpful? 0
  • +
  • -

#7 realNoName   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Random number between 2 ints

Posted 30 September 2008 - 03:39 PM

your problem is with your if statement

with this if you are saying if num1 is the bigger number then set num1 as lo when you should be setting it as hi because its larger
 if (num1 > num2)
      {
        lo = num1;
        hi = num2;
      }
    else
      {
        lo = num2;
        hi = num1;
      }


so just switch your sign to <
 if (num1 < num2)
      {
        lo = num1;
        hi = num2;
      }
    else
      {
        lo = num2;
        hi = num1;
      }

This post has been edited by realNoName: 30 September 2008 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Re: Random number between 2 ints

Posted 01 October 2008 - 12:23 PM

Oh!

Gosh, thank you!

Dumb mistakes rock my world. <_<


Is there anyway that I can help on this site - obviously I'm not the most proficient, but is there anything I could do?

Thanks again!

-John
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6789
  • View blog
  • Posts: 31,323
  • Joined: 10-May 07

Re: Random number between 2 ints

Posted 01 October 2008 - 12:28 PM

This is how I get random values between two given digits:

#include <stdio.h>

int getrand(int min, int max);

int main(void) {
  printf("Min : 1 Max : 30 %d\n",getrand(1,30));
  printf("Min : 100 Max : 1000 %d\n",getrand(100,1000));

  return 0;
}

int getrand(int min,int max){  
     return(rand()%(max-min)+min);  
}  



Was This Post Helpful? 0
  • +
  • -

#10 Gilthanis   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 08

Re: Random number between 2 ints

Posted 06 October 2008 - 11:59 AM

Actually, the max-min doesn't give you the bounded region, so if you wanted a number (inclusive) from 1 - 10 and did

rand() % (max - min) + min;


You would code
rand() % (10 - 5) + 5;


Which would basically be %5. No number %5 can = 5, so you will never get 10 as a random value.
To change this, all you have to do is find the range, which would be

max - min + 1

So a true random between 1 - 10 would be...

range = 10 - 5 + 1;
// range = 6
rand() % (range) + 1;



I think that's right.

- John

This post has been edited by Gilthanis: 06 October 2008 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 jgar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 31-August 12

Re: Random number between 2 ints

Posted 31 August 2012 - 07:27 PM

View PostBetaWar, on 30 September 2008 - 03:19 PM, said:

do something like so:
(rand()%(max-min))+min;

Hope that helps.


View PostBetaWar, on 30 September 2008 - 03:19 PM, said:

do something like so:
(rand()%(max-min))+min;

Hope that helps.


Thank you so much!!!:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1