• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

random number in c++

i need to get a random number between 0 and 3 (or 1 and 4, whatever works) in visual c++ 6.0.

what header files do i include? and what is the syntax for the function? i remember it being rand() but it doesn't seem to work with my headers that i've included.

all help would be appreciated. i'm rusty on c++ (too much Perl).
 
while i haven't touched c++ in a long long time, have you tried:

math.h
stdlib.h?
 
The following might help....

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

void main (void)
{
int i;

for(int i=0; i<10; i++)
{
cout << rand() % 4 << endl; //Generate a random number between 0 and 3
cout << (rand() % 3) + 1 << endl; //Generate a random number between 1 and 3
}

}
 
From the MSDN Library:

rand
Generates a pseudorandom number.

int rand( void );

Routine Required Header Compatibility
rand <stdlib.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

rand returns a pseudorandom number, as described above. There is no error return.

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.

Example

/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void main( void )
{
int i;

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( &quot; %6d\n&quot;, rand() );
}


Output

6929
8026
21987
30734
20587
6699
22034
25051
7988
10104





🙂atwl
 
Remember to srand() before calling rand() or you will get the same sequence of numbers every time..Regards
 
think this'll work for you...

#include <random.h>

in the &quot;int main&quot; put in the randomize() command. Then do random(3) to get a random number between 0 and 3. [This should work with the defaults in microsoft visual C++]
 
how random do you want the stuff to be?

if you need to run two instance of your code together you better not use time as the source of your srand, or else they will generate the same random number.

Remember, there is no such thing as truly random, you can only get as close as you can.

If you are working in *nix, you can use srand with your process id, i dont know about windows.
 
Back
Top