• 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 Numbers in c++:: update:: solved

Gooberlx2

Lifer
Hi,
I need to generate a random number between 0 and 1 using c++.

i figured it would simply be rand()%1 (seeded with the system clock ) from the stdlib.h, but that always gives me back 0.

Does rand() give back a double? ('cause I know RAND_MAX is huge).

All I need is any decimal between 0 and 1.

Any help is appreciated.

Thx
 
I figured it out. apparently I didn't know that RAND_MAX was an int, so when I tried:

srand( time(NULL) );
float j = rand() / RAND_MAX;
cout << j << endl;

it wouldn't work. I had to CAST it. (damnd such an easy solution)

srand( time(NULL) );
float j = (float)rand() / (float)RAND_MAX;
cout << j << endl;

Yay! 🙂
 
another way to do real Random Numbers is to download randgen.h and randgen.cpp (just type it in on google). The way it works is (make sure you add randgen.cpp as a node to your executable):

#include <iostream.h>
#include "randgen.h"

int main()
{
double x = 0;
RandGen r;
x = r.RandReal(0, 1);
cout << x << endl;
return 0;
}

This will give you a random real number between 0 and 1.
 
Back
Top