Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:56:08+00:00 2026-05-28T07:56:08+00:00

I have a set of elements and i need to choose any one element

  • 0

I have a set of elements and i need to choose any one element out of it. Each element is associated with a percentage chance. The percentages add to 100.

I need to choose one out of those element so that the chances of an element being chosen is equal to the percent value. So if a element has 25% chance, it is supposed to have 25% chances of getting chosen. In other words, if we choose elements 1 mil times, that element should be chosen near 250k times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:56:09+00:00Added an answer on May 28, 2026 at 7:56 am

    What you describe is a multinomial process.

    http://en.wikipedia.org/wiki/Multinomial_distribution#Sampling_from_a_multinomial_distribution

    They way to generate such random process is like this:
    ( I’ll use pseudo code but it should be easy to make it in to real code. )

    1. Sort the ‘boxes’ in reverse order of their probability:
      (not needed. it’s just an optimization)
      so that you have for example values=[0.45,0.3,0.15,0.1]

    2. then create the ‘cumulative’ distribution, which is the sum of all elements with index <=i.
      pseudocode:

      cumulant=[0,0,0,0]    // initiate it
      s=0
      for j=0 to size()-1 {
         s=s+values[i] ; 
         cumulant[i]=s
      }
      

      in our case cumulant=[0.45,0.70,0.85 ,1 ]

    3. make a uniform random number x between 0 and 1.
      For php: http://php.net/manual/en/function.rand.php

    4. the resulting random box index i is

      the highest i for which cumulant[i]< x

    pseudocode:

    for j=0 to size()-1 {
      if !(cumulant[i]<){
         print "your index is ",i
         break;
      }
    

    that is it. Get another random index i by going back to point 3.

    if you sort like suggested above, that means that the final search will be faster. For example, if you have this vector of probabilities: 0.001 0.001 0.001 0.001 0.996 then, when you sort it, you will almost always only have to look only at index i=0, since the random number x will almost always be lower than 0.996. If the sort pays off or not depends on if you repeatedly use the same ‘boxes’. So, yes with 250k tries it will help a lot. Just remember that the box index i you get is for the sorted vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have set of elements. They have several classes on one element. <div
I have a list of elements, each one identified with a type, I need
I have n elements that need to be partitioned into x sets, each set
I have a vector of booleans. I need to set its elements from n-th
I have a set of selected elements in jQuery: <div class=element></div> <div class=element></div> <div
I have a vector containing n elements. I need to choose a subset of
I have a set of elements of size about 100-200. Let a sample element
I need to choose one class from the parent element that I've clicked in
If have a set of collapsible elements, in which I need to replace classes
Ok I have a dynamically created set of elements where each particular kind gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.