OK
An other problem which I really need assistance with
I'm using srand to generate random numbers out of a range BUT i always get the same sequence of numbers!! But I notice the problem come from rand()
i tried to create a new project with only this :
#include <ctime>
#include <iostream>
using namespace std;
void main()
{
srand(time(NULL));
cout << rand() << endl;
cin.get();
cin.ignore();
}
rand() does give me 7880 - 7920 - 7934 - 7946 as time goes. I even tried closing VS completly and try again, i Get approximatly the same result.
What's wrong !

rand() not being random!!!
mccdan
// Usually, you will want to generate a number in a specific range, // such as 0 to 100, like this:
{
int RANGE_MIN = 0;
int RANGE_MAX = 100;
for (i = 0; i < 10; i++ )
{
int rand100 = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
printf( " %6d\n", rand100);
}
}
ReneeC
Both of the other people who have told you how to clamp the values have told you the hard way. The % operater returns the remainder from a divide, which means it returns you a number less than the number you specify. IE 31 % 30 == 1. Therefore, rand() % 50 will return a number between 0 and 49 and rand() % 30 + 20 will return a number between 20 and 49. That's how you clamp your random number range.
walt13671
Yeah, I do this for my main program, doesn't change a thing except I don't get warning message this time ;)
thanks for your help folks, I got it repaired (i just wonder why it worked several days ago... :P)
EDIT : My other question still lingers tho :
Can I set the Maximum value above 32000
Rag_Singh
Following Christians great advice using modulo operator. This will give a number between two limits, 1 to 999999.
int low = 1;
int high = 999999;
int random = (rand() % (high - low)) + low;
Prabhu R S
You should seed it only once, then generate your sequence in a loop.
motleycrazy
srand( (unsigned)time( NULL ) );
rclenzi
How can a plus operator NOT change the values I suggest you run the code you've been given in a loop, with an assert or other code that will alert you if it ever returns a value outside the range you expect. Just to make clear what should be obvious by looking at the code. The modulo operator will clamp a value to a range that starts at 0. Adding something to that will increase the value, which increases both the min and max of the possible range. If you have a number between 0 and 10, and you add 10 to it, you don't know what it is, but you know it's between 10 and 20.
TejasLambu
Not sure what you expect.
void initrand()
{
srand(time(NULL));
}
int randomlimits(int low, int high)
{
return random = (rand() % (high - low)) + low;
}
void testrandomlimits()
{
initrand();
for(int i = 0; i < 100; i++)
{
cout << randomlimits(1, 64000);
}
for(int i = 0; i < 100; i++)
{
cout << randomlimits(100, 200);
}
}
technoxicated
i copied your Exact code mr. Andreas and I've never seen a number going above : 32560 or below 730
I should be getting AT LEAST one number out of an hundred above the upper half of 64000
Edit : a little question : How can I specify a folder to my output filename
out.open(cBuffer, std::ios::beg);
how do I make it so it gets into \Accounts\<AccountID>
Edit 2 : Thx
cgraus for that answer, Patched my problem (i wont say fixed because I stick to rand() instead of getting a better randomizer :P)ostridge
Yeah, like I said, you need to seed it once and generate a sequence.
You can use % to specify a max value. As in rand() % 32000
Imorti
rand() gives pseudo random numbers, it means it constructs a random number from the seed that is given.
You will see a pattern because of this.
srand() sets the seed but if the seed is similar to the previous seed you will see "random" numbers that are close to each other. Even after a few iterations.
rand() returns a unsigned int.
to get a number in a certain range you can use this formula
rand() / 4294967295 * yourrangemaximum
4294967295 is what can be stored in a int on 32 bit computers
Expressed as 0xffffffffin hexadecimal
BCDC
but i'm not sure it can change the values
help_seek
i'll try to loop
Edit : Oh Damn.. Can I see the inner working of rand
it works but not for the first randomized number.
So i guess i should try and trash the first rand() value and get the others
Edit 2 : is there something i can do if i need a number between 1 and 9999999 or the maximum is 32,000 something
kg_masterpiece
You should be aware that rand() is pretty poor random number generator - if you need a real random generator you take a look at Boost library:
http://www.boost.org/libs/random/index.html