Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a random C function with weighted variables

Status
Not open for further replies.

herky1978

Computer
Apr 20, 2005
1
0
0
US
Does anyone know of a C random function that allows for weighted variables? For example...

I would like to assign variable a random value between 1 and 5. However, I would like for this value to be certain numbers more frequently than others. So I would like to be able to use a function that will randomly select the value 1 - 20% of the time, 2 - 25% of the time, 3 - 30% of the time, and so on.
 
Replies continue below

Recommended for you

To randomly select an integer between 1 and 5, you might run a random number generator that returns a value along some finite number line; floats 0..1, integers 0..65535, etc, and divide that random number line evenly to 'bin' the rng output and map it to your integer output.

To randomly select an integer between 1 and 5, but with biased output, you use the same rng, but you divide the number line unevenly.

That's one way.




Mike Halloran
NOT speaking for
DeAngelo Marine Exhaust Inc.
Ft. Lauderdale, FL, USA
 
Status
Not open for further replies.
Back
Top