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 6907699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:28:04+00:00 2026-05-27T08:28:04+00:00

I have a simple array (or set if you prefer) of integers, let’s call

  • 0

I have a simple array (or set if you prefer) of integers, let’s call it X. I also have another array W that stores the “weight” of elements in array X. The “weight” indicates how likely n-th element should be selected. Now I need a method (algorithm) to (pseudo) randomly select one element from array/set X according to its “weight” defined in array W.

For example, if my W looks like this:
W[0] = 2;
W[1] = 4;
W[2] = 6;

that means probability of selecting N-th item from array X is:
X[0] = 16.6%
X[1] = 33.3%
X[2] = 50%

so method get_pseudorandom_item(X) should return 2-nd item around half of all times.

Any ideas or suggestions how to implement this (in any programming language) are much appreciated.
Thanks.

  • 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-27T08:28:05+00:00Added an answer on May 27, 2026 at 8:28 am
    1. Generate an array P with the partial sums of the weights, i.e.

      (P0 = W0), (P1 = W0 + W1), …, (Pn = W0 + W1 + … + Wn)

      (you can do that within W, actually, if you don’t need the weights afterwards).

    2. Generate a random number r in [0, Pn) where Pn denotes the last such sum (i.e. the sum of all weights).

    3. Find the index k of the smallest (first) partial sum larger than the number you generated:

      Pk > r  ∧  ∀ a < k: Pa < r

    4. Use that index to select your actual element: Xk

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

Sidebar

Related Questions

I have a simple simulated array with two elements: bowl["fruit"] = "apple"; bowl["nuts"] =
I have a simple one dimmensional array of integer values that represent a physical
Let`s suppose we have a simple (non-assoc) array with 100001 values and these values
I have a simple OpenGL program and trying to draw an instanced array that
I have a simple array that I need to serialize as part of a
I can't set a variable from a post array. I have a simple form
I have a simple array with x number of items. I am displaying them
I have a simple array of stuff: $array = array(apples,oranges,strawberries); I am trying to
I have a simple 2D array of strings and I would like to stuff
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]

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.