Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-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
Joined
Apr 20, 2005
Messages
1
Location
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.
 
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.

Part and Inventory Search

Sponsor

Back
Top