random number in c++

Mar 5, 2001
66
0
0
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).
 

rominl

Moderator Emeritus<br>Elite Member
Nov 2, 2000
849
0
0
while i haven't touched c++ in a long long time, have you tried:

math.h
stdlib.h?
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
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
}

}
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
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
 

Cyph3r

Senior member
Jan 20, 2000
790
0
0
Remember to srand() before calling rand() or you will get the same sequence of numbers every time..Regards
 

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
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++]
 

PandaBear

Golden Member
Aug 23, 2000
1,375
1
81
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.