Sign in to follow this  
Guest bartels

Creating Better Random Number with C++ and the MingGW Compiler

Recommended Posts

Hi, in one of my gauges I need to create a random number between 1 and 100.This is my code so far. time1 = CLOCK_MINUTEvar.var_value.n ; int seed = time1; srand(seed); random= (rand()%100);It works ok, but the the generated "random" number isn't really that random. For example, if the Clock_Minute variable equals 44 minutes, the "random" generated number is always 82. Can anyone think of a way to bolster the "randomness" of this code?I'm using the MingGW compilier with the Easy Gauge Programmer's Ed.Thanks, Bryan

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

You might try using the last two digits of TICK18. Again, this is not completely random, but probably close enough.Doug

Share this post


Link to post
Share on other sites

As far as I know you shouldn't "reseed" the random sequence every time, especially no with the same value. I assume you only need to use srand once at gauge loadup, or at least not with repitive numbers (e.g. srand((unsigned)time( NULL ) ); or ELAPSED_SECONDS instead of CLOCK_MINUTE). Arne Bartels

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this