Linked Questions

0 votes
1 answer
3k views

Possible Duplicates: Random weighted choice Select random k elements from a list whose elements have weights Let's say I have a set of N objects, set = range(a,b) = [a,a+1,...b-2, b-1] To ...
David542's user avatar
  • 112k
220 votes
36 answers
180k views

I need a quick algorithm to select 5 random elements from a generic list. For example, I'd like to get 5 random elements from a List<string>.
JC Grubbs's user avatar
  • 40.5k
81 votes
14 answers
72k views

I would like to randomly select one element from an array, but each element has a known probability of selection. All chances together (within the array) sums to 1. What algorithm would you suggest ...
Mikulas Dite's user avatar
  • 7,951
54 votes
9 answers
36k views

Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for ...
Nick Johnson's user avatar
40 votes
6 answers
54k views

Given a list of probabilities like: P = [0.10, 0.25, 0.60, 0.05] (I can ensure that the sum of all the variables in P is always 1) How can I write a function that randomly returns a valid index, ...
Roughmar's user avatar
  • 425
21 votes
12 answers
22k views

I have a bunch of keys that each have an unlikeliness variable. I want to randomly choose one of these keys, yet I want it to be more unlikely for unlikely (key, values) to be chosen than a less ...
20 votes
4 answers
27k views

How to randomly pick up N numbers from a vector a with weight assigned to each number? Let's say: a = 1:3; % possible numbers weight = [0.3 0.1 0.2]; % corresponding weights In this case probability ...
yuk's user avatar
  • 19.9k
50 votes
4 answers
9k views

This question led to a new R package: wrswoR R's default sampling without replacement using sample.int seems to require quadratic run time, e.g. when using weights drawn from a uniform distribution. ...
krlmlr's user avatar
  • 25.6k
18 votes
5 answers
13k views

I have a list of 100,000 objects. Every list element has a "weight" associated with it that is a positive int from 1 to N. What is the most efficient way to select a random element from the list? I ...
John Shedletsky's user avatar
9 votes
7 answers
23k views

I'm looking for a reasonable definition of a function weighted_sample that does not return just one random index for a list of given weights (which would be something like def weighted_choice(weights,...
Anaphory's user avatar
  • 6,482
7 votes
4 answers
4k views

Update: my problem has been solved, I updated the code source in my question to match with Jason's answer. Note that rikitikitik answer is solving the issue of picking cards from a sample with ...
Gabriel's user avatar
  • 1,652
9 votes
1 answer
4k views

Lets say we have some discrete distribution with finite number of possible results, is it possible to generate a random number from this distribution faster than in O(logn), where n is number possible ...
Tomek Tarczynski's user avatar
1 vote
5 answers
4k views

I have a population p of indices and corresponding weights in vector w. I want to get k samples from this population without replacement where the selection is done proportional to the weights in ...
petrichor's user avatar
  • 6,569
3 votes
2 answers
2k views

Suppose I have a big 2D array of values in the range [0,1] where 0 means "impossible" and 1 means "highly probable". How can I select a random set of points in this array according to the ...
Nicolas Repiquet's user avatar
3 votes
3 answers
1k views

I've got a problem I'm not too sure how to solve. I have a 2d space with several points in it. I also have a current point, which is one of the points in the space. I want to randomly select one of ...
Graham's user avatar
  • 1,084

15 30 50 per page