rand not applying correctly?

Hi I am trying to generate a random number between 1 - 100 using the example on this site. It works...sort of, except the number is never between my given range.

CODE:
srand(time(NULL));
distance = rand() % 100 + 1;

I always get numbers around 270-390. I have not set this anywhere.
When i tried to get a number between 200-250 i used:

srand(time(NULL));
distance = rand() % 250 + 200;

This gave numbers like 350-490.

What have I missed?
srand(time(NULL));
distance = rand() % 100 + 1;
I always get numbers around 270-390.

This I don't understand.

srand(time(NULL));
distance = rand() % 250 + 200;
This gave numbers like 350-490.
rand() % 250 returns a value 0-249 so rand() % 250 + 200 should give you a value 200-449.

I think you better show some real code.
Thanks for your response. I am at work now so i dont have my project handy. Its a simple console app in vsc++ 2008 express edition.

Judging by the first lot of code, I should get a number between 1-100 right?

I dont, but then why dont i get a number between 200-250 with the second lot of code?

If i replace the 1 with 200, and 100 with 250 should it not produce a range of 200-250?
If you want a random number from a to b, it's
rand() % (b - a + 1) + a

So from 200-250, it's
rand() % 51 + 200

If you didn't get a number between 1 - 100 with
rand() % 100 + 1,
then check to make sure you didn't make a typo.
Last edited on
Heres my code:

//Golf game test
#include <iostream>
#include "time.h"
using namespace std;

int main (void) {
int firstshot, distance;

srand(time(NULL));
distance = rand() % 51 + 200;


//First hole
cout << "Hole 1: 554m : Par 5" << endl;
cout << "1. Driver, 2. 3 Wood, 3. Iron" << endl;
cin >> firstshot;
//If you chose driver you will be penalised
if(firstshot==1){
cout << "You landed in the rough!" << endl;

cout << distance << " meters to hole" << endl;
}
//If you chose 3 Wood or iron lucky you
else {
cout << "You landed in the fairway!" << endl;
}

cout << "1. 3 Wood, 2. Iron, 3. Wedge" << endl;

//pausing game so it doesnt quit
system("pause");
return 0;
}

Its still not giving numbers in the range i specify.
distance is between 200-250. Isn't that what you want?
Yes but the output is not always between 200-250. With this code the first three attempts were 241, 389, 357.

Is there something wrong with my code?

Also when i had 100 + 1 in there i kept getting the same sort of range about 250-500.

Would just like to know if I have done something wrong?
Last edited on
No the code is correct. Are you sure you have compiled the code and running the correct program?
try this instead, it is my preferred method of generating a random number, although I've simplified the general structure of it for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//your includes

int randomize(int max = 99, int min = 1)
{
    // don't forget to seed once in main.
    max++;
    max -= min;
    return (rand() % max + min);
}

int main()
{
    //blah blah blah
    distance = randomize(250, 200);
    //blah blah blah
}


see if that works.
Last edited on
Ive just been rebuilding my file, and then debugging. Its only in the console, anything else I should do?
what IDE are you using? Dev-C++ is out of date and may be the cause of the problem.
Microsoft VS C++ 2008 the free edition
I don't know much about VS, but I do know 2008 isn't the latest version, see if the newer one fixes the problem, I believe it is 2010.
I may have too. I was recommended this version because it was free from MS. I may just start a new project and see if that fixes it first.
Thanks for your help
What do you get as output for this program?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(0));
    for (int i = 0; i < 10; ++i)
        cout << rand() % 51 + 200 << '\n';
    return 0;
}
isn't the 2010 version also free? if you are looking for a good free compiler, I recommend Code::Blocks.
Ok ill admit im a total noob but something funny is up. Possibly my skills.

Long Double , I copied and pasted your code over my existing code, rebuilt the file and went to debug. I am still getting my golf info asking me for input. Which it again gave me 300 as the number.

Do I assume I need to compile the program another way?
Err, I meant to compile that program by itself (don't copy/paste it into anything) and tell me what you get as output.

For instance, with MSVC++ 2010, I get something like this:
216
204
208
230
211
224
226
223
234
234
Ok i put it in a new project and ran the debug.
Yes i got a range of numbers between 200-250.

Your formula is a little different to what I had, would you mind explaining what the ++i and the '\n' mean?
++i just adds one to i, \n simply means end the line, so that you don't get all the numbers showing on a single line (at least until it wraps around the console window)

Topic archived. No new replies allowed.