I'm a college student majoring in the Computer Science field, and this was an assigned project for a class. It's nothing more than a test to show the efficiency of different sorting algorithms and the efficiency difference between sequential and binary searches for 10,000,000 numbers. All of that I understand, and executed perfectly using the STL.

The project requested that the range of the random numbers generated be 0 - 99,999. Of course, rand() returns unsigned int, if I recall correctly, which would max out to 32,727 (under the maximum of the range) as it showed in my testing. I ended up using the Mersenne Twister Random Number Generator in my program, but as a junior in college this seemed a bit extreme. I tried using typecasts on my srand and rand functions but could not overwrite the return type for rand(). This is what I tried:

srand((unsigned long int)(time(NULL)));

and, in using a vector to store my numbers and a For loop...

for(unsigned int i = 0; i < sortMe.size(); i++)
[INDENT]sortMe[i] = ((unsigned long int)rand()) % 100000;[/INDENT]

I know that unsigned long int (and, really, long int) would hold a value that exceeds 99,999 but regardless of how and where I typecasted srand() and rand() the value would never exceed 32,767. Is it possible to override the int rand() function in the cstdlib (stdlib.h)? I even attempted:

#undef RAND_MAX
#define RAND_MAX 100000

to override the value of RAND_MAX but still, the return type of int on rand() is preventing this. Any help would be appreciated as I cannot see how my professor would want a junior level college student to random that range of numbers w/o any knowledge of "randomizing" outside of the rand() function.

If you'd like to see my complete project, I'll gladly post it.

9 Years
Discussion Span
Last Post by theories

Thanks for the reply.

I attempted to use

sortMe[i] = ((rand()) << 16 + (rand())) % 100000;

And it did properly adjust the rand() to give me the range I needed, but I'm now getting essentially -99999 - 99999 or thereabouts. How would I go about adjusting to remove the negatives?


You could take the abs( ) of the generated number. Or how about:

unsigned int num;
num = rand( );
num = num << 16 ;
num += rand( );
num %= 100000;
sortMe[i] = (unsigned long int)(((rand()) << 16) + (rand())) % 100000;

This worked. I took the idea of the unsigned int num and stepping through and applied a type cast to the statement. I appreciate you taking the time to assist me.

So this is basically stating that I take a rand(), left shift it 16 bits, and then add another rand() to it to create a 32 bit number, and then the remainder of a 100,000 mod operation (to get 0 - 99,999) will be the resulted "random" number?

And, through a typecast, would this be more or less random than just doing an abs() of the result and then storing it, since all negative numbers will result in a 0 through typecasting (unless I'm mistaken on this).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.