Jump to content
  • Advertisement
Sign in to follow this  
Dko

rand()

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya. Im attempting to make a simple guess a number game in wich the comptuer guesses the number. Right now the 'Ai' if you would call it that really sucks. It just randomly picks a number every time. I have an idea to make it much better but I was wondering how can I make rand() return a number thats between two sets of numbers not just '1 thru #'?

Share this post


Link to post
Share on other sites
Advertisement
Take your

highest possible number;
lowest possible number;

to get a number in this range.

random number = lowest + rand() % (highest + 1);

and you should

srand((unsigned)time(0));

before calling rand() above just to give rand() a different starting point (the time) before it returns a number to you.

Share this post


Link to post
Share on other sites
Just something you should know. Make sure you only call srand() one time during the duration of the program. If you call it more than once you'll keep getting the same random number. Put it at the top of you main() loop or something so it only gets called once.

Share this post


Link to post
Share on other sites
Thanks NumberXaero. A little more info then I needed. (Knew about the srand and what not) But now I can finnaly make my games Ai at least as smart as me... if not more so. lol

Share this post


Link to post
Share on other sites
No problem, and a way to get around the srand(time) giving the same number for the current run would be to call a Sleep() type function after each srand() call, thus allowing some amount of time to pass between calls (not taking into account the side effects of such a call on the program, there would be a pause depending on how long you decide to wait)

Share this post


Link to post
Share on other sites
Ok new prob. For some reason its guessing numbers over my high number. >< When the numbers guessed should get smaller and closer to the actual number.

Currently this is what my code looks like this

// Guess my number
// The clasic number guessing game

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

using namespace std;

int main()
{
srand(time(0)); // seed random number generator

int theNumber; // random number betwenn 1 and 100
int tries = 0, guess;
int lowNum = 0, highNum = 100;


cout << "\tWelcome to Guess My Number\n\n";
cout << "Enter a number for the computer to guess: ";
cin >> theNumber;

do
{
guess = lowNum + rand() % (highNum +1);
++tries;

cout << "The computer picked." << guess << endl;
if (guess > theNumber)
{
cout << "Its too high, Doh!!\n\n";
highNum = guess;
}

if (guess < theNumber)
{
cout << "Its too low, Doh!!\n\n";
lowNum = guess;
}



} while (guess != theNumber);

cout << "\nThat's it! The computer got it in " << tries << " guesses!\n";

return 0;
}

Share this post


Link to post
Share on other sites
Ah. The problem is in the way the rand statement is set up.

random number = lowest + rand() % (highest + 1);
is only partially correct.

It really works like this.
random number = lowest + rand() % (range + 1);

Where range is the highest number minus the lowest number.

If you deconstruct the statement, it makes sense.

We start with the lowest number with the intention of adding a positive integer, or 0. If you don't know already, % is the modulus division operator, or the remainder operator. This guarantees that the result of rand() is limited to a number >=0 and less than the last parameter, since it's technically a remainder.

What we want for this last parameter isn't the maximum number, but the maximum range of numbers, representing the set of numbers allowed between the lowest and highest allowed guesses.

So, in conclusion, change your rand statement to this...

guess = lowNum + rand() % (highNum - lowNum +1);

Share this post


Link to post
Share on other sites
Sweet! and with the little Sleep() trick it now works great. Thanks everyone. With this site and my books C++ is finnaly starting to clikc :3

Share this post


Link to post
Share on other sites
Here's a little widget I use for this...

# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;

int random(int,int);

int main()
{
srand((unsigned int)time(NULL));
int randInt;
randInt = random(1,50);
cout << randInt << endl;
return 0;
}

int random(int min, int max)
{
return (int)((max - min) * rand()/(float)RAND_MAX + min);
}



Returns a random integer with a customizable min and max value. Its a little more involved but is supposed to have a more even distribution than the modulus method.

Quote:
Original post by NumberXaero
No problem, and a way to get around the srand(time) giving the same number for the current run would be to call a Sleep() type function after each srand() call, thus allowing some amount of time to pass between calls (not taking into account the side effects of such a call on the program, there would be a pause depending on how long you decide to wait)


Why would you ever need to call srand more than once?

Share this post


Link to post
Share on other sites
On a sort of almost related note, sranddev is a function that auto-seeds based on time, with no extra function call. It saves like 4 key-strokes. Wow.

Also there is actually a function random (and conversly srandomdev), which might be a more even distribution? Not sure.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!