Public Group

Random Numbers

This topic is 5202 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Im reading the book beginning C++ proggraming and it does not explain very well how the formula works for creating random numbers, it just tells me that it works. Here is 2 examples: int die = (randomNumber % 6) + 1; // creats number between 1 and 6 theNumber = rand() % 100 + 1; //random number betwwen 1 and 100 I would like to understand why this happens so I can make code that for example creates random numbers between 1 and 33 or 45 and 60. Thaks :)

Share on other sites
Modulo [the % operator] is used for finding the "remainder" of division. 5/3==1 5%3==2.

In more complex terms, the modulo reduces a number to its position on a finite number line. Thus to make a finite number line, the modulo operator is used.

x % 5 means that 0,1,2,3,4 are the only possible results. A finite number line. To move that finite number line, simply add or subtract the offset. (x % 5) + 4 means that 0+4,1+4,2+4,3+4,4+4 are the only results possible [4-8].

Note that (x+4) % 5 will still only result in [0-4] as possible results.

Share on other sites
Thanks for the awsome post, I thought I understood it untill I got these crazy reuslts.

Im trying to make a game were you pick a number 1-100 and then the computer randomly pics a number. Then i want it to say that if the number picked is to high or low. For example if I chose the number 33 and the computer picks 56, I wanted it to make it say, now pick a number between 1-55. This is my code that I have looked at over and over and cant find out why it does not work.

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

using namespace std;
int main()
{
srand(time(0));

int myNumber;
int computerGuess;
int tries = 0;
int base = 100;
char again = 'y';

while (again == 'y')
{

tries = 0;
cout << "\n\nChose A Number For The Computer To Guess from 1-100: ";
cin >> myNumber;

do
{
computerGuess = rand() % base + add;
++tries;

cout << "\n\n" << computerGuess;

if (computerGuess > myNumber)
cout << "\n\nThe Computer Guessed To High And Tried Again";
base = 100;
base = computerGuess - 1;

if (computerGuess < myNumber)
cout << "\n\nThe Computer Guessed To Low And Tried Again";
base = 100;

} while (computerGuess != myNumber);

cout << "\n\nIt Took Your Computer " << tries << " guesses to get the your number";
cout << "\n\nWould you like to try again? (y/n): ";
cin >> again;
}

cout << "\nOkay, CYA!!!";
return 0;
}

// The problem is for some reason when it says... the computerGuess is bigger then myNumber I should pick a higher number. I am getting resluts like this, I pick 44 and it picks 56 then 67 then 72 then 92 then 99 and 100 then it dies... lol I appreiciate your help :)

Share on other sites
I'm a little confused here, sorry. Are you saying you want the player to choose a number and then let the computer try to guess at it?

Share on other sites
The app. crashes because the variable base is getting set to 0. Here is the fatal line:
computerGuess = rand() % base + add;

You never want to mod by 0. There are many ways you can correct this error so I will leave that up to your discretion.

CAB

Share on other sites
Here is what I think you want. If not you can only learn from it:

#include <iostream>#include <cstdlib>#include <ctime>using namespace std;unsigned int gPlayerNum = 0;unsigned int gCompGuesses = 0;unsigned int gatherNum( void );void compGuessNum( void );void results( void );short int again( void );int main( void ){	int answer = 0;	do	{		gPlayerNum = gatherNum( );		compGuessNum( );		results( );		gCompGuesses = 0;		answer = again( );	} while ( answer != 2 );		return 0;}unsigned int gatherNum( void ){	unsigned int temp = 0;	puts( "Welcome. Choose a number between 1 and 10." );	cin >> temp;	return temp;}void compGuessNum( void ){	unsigned int temp = 0;	srand( time( NULL ) );	puts( "Now the computer is going to guess the number you just picked. Please wait..." );	do	{		temp = ( rand( ) % 10 ) + 1;		gCompGuesses += 1;	} while ( temp != gPlayerNum );	return;}void results( void ){	cout << "The computer took " << gCompGuesses << " guess(es) to find out your number (" << gPlayerNum << ")." << endl;	puts( "Have a nice day!" ); // programs should always be nice	return;}short int again( void ){	short int temp = 0;	puts( "Do you want to play again? [1] for yes and [2] for no." );	cin >> temp;	while ( temp != 1 && temp != 2 )	{		puts( "Huh?" );		cin >> temp;	}	return temp;}

I lowered it down to 10 for 100 just took way to long.

[Edited by - V E C T O R on August 21, 2004 9:43:58 PM]

Share on other sites
Scrime show me this code here:
(works great for me, thank you again!)

credits to Scrime!
namespace random {	template<class T>	class TRandom	{	public:		static T MinMax(T minimum, T maximum) {			// do a bit of sanity on the min/max values			if( minimum == maximum ) 				return minimum;			else if( minimum < maximum )				return static_cast<T>( ((static_cast<float>(rand()) / static_cast<float>(RAND_MAX+1)) * (maximum-minimum)) + minimum);			else				return static_cast<T>( ((static_cast<float>(rand()) / static_cast<float>(RAND_MAX+1)) * (minimum-maximum)) + maximum);		}	};}#ifndef RANDOM_FLOAT#define RANDOM_FLOAT(min, max) (random::TRandom<float>::MinMax((min),(max)))#endif#ifndef RANDOM_INT#define RANDOM_INT(min, max) (random::TRandom<int>::MinMax((min),(max)))#endif#ifndef RANDOM_DIE#define RANDOM_DIE(min, max) (random::TRandom<int>::MinMax((min),(max)+1))#endif#ifndef RANDOM_DOUBLE#define RANDOM_DOUBLE(min, max) (random::TRandom<double>::MinMax((min),(max)))#endif

use:
const double rnd_double = RANDOM_DOUBLE( 1.11, 11.11 );const int rnd_int = RANDOM_INT( 1, 100 );

Credits to Scrime! :)

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• Forum Statistics

• Total Topics
633379
• Total Posts
3011570
• Who's Online (See full list)

There are no registered users currently online

×