×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to create a random C function with weighted variables

How to create a random C function with weighted variables

How to create a random C function with weighted variables

(OP)
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.

RE: How to create a random C function with weighted variables

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources