All Questions

Tagged with
Filter by
Sorted by
Tagged with
308 votes
23 answers
251k views

Generate a random point within a circle (uniformly)

I need to generate a uniformly random point within a circle of radius R. I realize that by just picking a uniformly random angle in the interval [0 ... 2π), and uniformly random radius in the ...
aioobe's user avatar
  • 417k
148 votes
4 answers
15k views

Data structures for loaded dice?

Suppose that I have an n-sided loaded die, where each side k has some probability pk of coming up when I roll it. I’m curious if there is a good data structure for storing this information statically (...
templatetypedef's user avatar
140 votes
3 answers
10k views

Why does this random value have a 25/75 distribution instead of 50/50?

Edit: So basically what I'm trying to write is a 1 bit hash for double. I want to map a double to true or false with a 50/50 chance. For that I wrote code that picks some random numbers (just as an ...
gvlasov's user avatar
  • 19.3k
79 votes
14 answers
41k views

Select k random elements from a list whose elements have weights

Selecting without any weights (equal probabilities) is beautifully described here. I was wondering if there is a way to convert this approach to a weighted one. I am also interested in other ...
nimcap's user avatar
  • 10.2k
76 votes
13 answers
72k views

How to pick an item by its probability?

I have a list of items. Each of these items has its own probability. Can anyone suggest an algorithm to pick an item based on its probability?
Ruzanna's user avatar
  • 9,816
68 votes
11 answers
86k views

Random number with Probabilities

I am wondering what would be the best way (e.g. in Java) to generate random numbers within a particular range where each number has a certain probability to occur or not? e.g. Generate random ...
marc wellman's user avatar
  • 5,846
45 votes
7 answers
74k views

Generate Random Boolean Probability

I only know how I can generate a random boolean value (true/false). The default probability is 50:50 But how can I generate a true false value with my own probability? Let's say it returns true with ...
Dark Side's user avatar
  • 705
38 votes
6 answers
50k views

How can I make a random choice according to probabilities stored in a list (weighted random distribution)?

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
  • 305
38 votes
10 answers
5k views

An interview question: About Probability

An interview question: Given a function f(x) that 1/4 times returns 0, 3/4 times returns 1. Write a function g(x) using f(x) that 1/2 times returns 0, 1/2 times returns 1. My implementation is: ...
Sawyer's user avatar
  • 15.7k
36 votes
7 answers
53k views

How do I simulate flip of biased coin?

In unbiased coin flip H or T occurs 50% of times. But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'. something like this: def flip(p): '''this ...
Pratik Deoghare's user avatar
30 votes
7 answers
35k views

Generate random integers with probabilities

I'm a bit confused about how to generate integer values with probabilities. As an example, I have four integers with their probability values: 1|0.4, 2|0.3, 3|0.2, 4|0.1 How can I generate these ...
Headshota's user avatar
  • 21.3k
30 votes
8 answers
26k views

Distributed probability random number generator

I want to generate a number based on a distributed probability. For example, just say there are the following occurrences of each numbers: Number| Count 1 | 150 2 | ...
Mark Conway's user avatar
30 votes
9 answers
13k views

How to do weighted random sample of categories in python

Given a list of tuples where each tuple consists of a probability and an item I'd like to sample an item according to its probability. For example, give the list [ (.3, 'a'), (.4, 'b'), (.3, 'c')] I'd ...
John's user avatar
  • 303
30 votes
8 answers
14k views

Random boolean with weight or bias

I need to generate some random booleans. However I need to be able to specify the probability of returning true. As a results doing: private Random random = new Random(); random.nextBoolean(); will ...
diestl's user avatar
  • 2,040
25 votes
7 answers
39k views

Generate random number with given probability matlab

I want to generate a random number with a given probability but I'm not sure how to: I need a number between 1 and 3 num = ceil(rand*3); but I need different values to have different probabilities ...
Eamonn McEvoy's user avatar
23 votes
5 answers
47k views

Algorithm to generate Poisson and binomial random numbers?

i've been looking around, but i'm not sure how to do it. i've found this page which, in the last paragraph, says: A simple generator for random numbers taken from a Poisson distribution is obtained ...
user avatar
23 votes
4 answers
24k views

Probability Random Number Generator

Let's say I'm writing a simple luck game - each player presses Enter and the game assigns him a random number between 1-6. Just like a cube. At the end of the game, the player with the highest number ...
Alon Gubkin's user avatar
  • 56.9k
21 votes
12 answers
21k views

Probability distribution in Python

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
7 answers
12k views

Unbiased random number generator using a biased one

You have a biased random number generator that produces a 1 with a probability p and 0 with a probability (1-p). You do not know the value of p. Using this make an unbiased random number generator ...
Rohit Banga's user avatar
  • 18.6k
19 votes
6 answers
12k views

Generating N numbers that sum to 1

Given an array of size n I want to generate random probabilities for each index such that Sigma(a[0]..a[n-1])=1 One possible result might be: 0 1 2 3 4 0.15 0.2 0.18 0.22 0.25 ...
Yuval Adam's user avatar
  • 163k
18 votes
2 answers
2k views

Effective Java Item 47: Know and use your libraries - Flawed random integer method example

In the example Josh gives of the flawed random method that generates a positive random number with a given upper bound n, I don't understand the two of the flaws he states. The method from the book ...
Derek Mok's user avatar
  • 313
17 votes
5 answers
11k views

Generate random numbers distributed by Zipf

The Zipf probability distribution is often used to model file size distribution or item access distributions on items in P2P systems. e.g. "Web Caching and Zip like Distribution Evidence and ...
dmeister's user avatar
  • 35.1k
15 votes
3 answers
8k views

Fast weighted random selection from very large set of values

I'm currently working on a problem that requires the random selection of an element from a set. Each of the elements has a weight(selection probability) associated with it. My problem is that for ...
user avatar
13 votes
6 answers
17k views

How to choose keys from a python dictionary based on weighted probability? [duplicate]

I have a Python dictionary where keys represent some item and values represent some (normalized) weighting for said item. For example: d = {'a': 0.0625, 'c': 0.625, 'b': 0.3125} # Note that sum([v ...
Joseph's user avatar
  • 13k
13 votes
4 answers
18k views

Chance of winning PHP percent calculation

I have a "battle" system, the attacker has a battle strength of e.g. 100, the defender has a strength of e.g. 75. But I'm stuck now, I can't figure out how to find the winner. I know the attacker has ...
Sims's user avatar
  • 177
13 votes
3 answers
22k views

c# probability and random numbers

I want to trigger an event with a probability of 25% based on a random number generated between 1 and 100 using: int rand = random.Next(1,100); Will the following achieve this? if (rand<=25) { ...
CdrTomalak's user avatar
13 votes
2 answers
5k views

Get true or false with a given probability

I'm trying to write a function in c++ that will return true or false based on a probability given. So, for example if the probability given was 0.634 then, 63.4% of the time the function would return ...
user2317084's user avatar
12 votes
1 answer
2k views

Arc4random modulo biased

According to this documentation, arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two. How ...
AJ222's user avatar
  • 1,124
12 votes
7 answers
9k views

How to implement Random(a,b) with only Random(0,1)? [duplicate]

Possible Duplicate: how to get uniformed random between a, b by a known uniformed random function RANDOM(0,1) In the book of Introduction to algorithms, there is an excise: Describe an ...
Jackson Tale's user avatar
  • 25.6k
12 votes
8 answers
2k views

What is O value for naive random selection from finite set?

This question on getting random values from a finite set got me thinking... It's fairly common for people to want to retrieve X unique values from a set of Y values. For example, I may want to ...
Beska's user avatar
  • 12.6k
12 votes
6 answers
4k views

How to generate correlated binary variables

I need to generate a series of N random binary variables with a given correlation function. Let x = {xi} be a series of binary variables (taking the value 0 or 1, i running from 1 to N). The marginal ...
jonalm's user avatar
  • 935
11 votes
3 answers
14k views

Probability of getting the same value using Math.random

The requirement is to send a unique id to database when user click on submit button. So I am using Javascript Math.random method. I just want to know what are the chances or possibility to get same ...
Jitender's user avatar
  • 7,761
11 votes
2 answers
5k views

Uniform distribution from a fractal Perlin noise function in C#

My Perlin noise function (which adds up 6 octaves of 3D simplex at 0.75 persistence) generates a 2D array array of doubles. These numbers each come out normalized to [-1, 1], with mean at 0. I clamp ...
Superbest's user avatar
  • 26k
10 votes
4 answers
55k views

Random numbers with different probabilities [duplicate]

I need to randomly determine a yes or no outcome (kind of a coin flip) based on a probability that I can define (.25, .50, .75). So for example, I want to randomly determine yes or no where yes has a ...
Josh Brittain's user avatar
10 votes
4 answers
9k views

Generate Random Numbers with Probabilistic Distribution

Ok, so here's my problem. We are looking at purchasing a data set from a company to augment our existing data set. For the purposes of this question, let's say that this data set ranks places with ...
ircmaxell's user avatar
  • 164k
10 votes
3 answers
8k views

Generate a uniformly random point within an annulus (ring) [duplicate]

Possible Duplicate: Create random number within an annulus I would like to obtain a uniformly obtained random point within an annulus, that is, the area that lies inside a circle of radius R1, ...
Ricardo Sanchez-Saez's user avatar
9 votes
3 answers
5k views

Random numbers based on a probability

I'm having trouble generating random numbers that do not follow a discrete uniform distribution. So for example, say I have 5 numbers (to keep it simple), a probability of number k being generated ...
DillPixel's user avatar
  • 955
9 votes
2 answers
6k views

C++ function for picking from a list where each element has a distinct probability

I have an array of structs and one of the fields in the struct is a float. I want to pick one of the structs where the probability of picking it is relative to the value of the float. ie struct s{ ...
Stuart's user avatar
  • 1,723
9 votes
1 answer
4k views

How to generate a random number from specified discrete distribution?

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
8 votes
3 answers
6k views

Returning a random value from array with probability proportional to it's value

I have an array like $keywords = array('apple'=>10,'orange'=>2,'grape'=>12); I want to randomly pick one of the "Key" from the array. However the probability distribution should be such ...
Eastern Monk's user avatar
  • 6,515
8 votes
7 answers
15k views

generate random numbers within a range with different probabilities

How can i generate a random number between A = 1 and B = 10 where each number has a different probability? Example: number / probability 1 - 20% 2 - 20% 3 - 10% 4 - 5% 5 - 5% ...and so on. I'...
Dan Dinu's user avatar
  • 33k
8 votes
1 answer
8k views

Python - modelling probability

I have a simple problem. I need a way to make a function which generates 0s in p percent cases and 1s in all other cases. I tried doing it with random.random() like this: p = 0.40 def generate(): ...
darxsys's user avatar
  • 1,570
8 votes
3 answers
640 views

re-using random number in reservoir sampling

It was asked in relation to another question recently: Given an unknown length list, return a random item in it by scanning it only 1 time I know you shouldn't, I just can't put my finger on a ...
Will's user avatar
  • 74.8k
8 votes
4 answers
6k views

how to numerically sample from a joint, discrete, probability distribution function

I have a 2D "heat map" or PDF that I need to recreate by random sampling. I.E. I have a 2D probability density map showing starting locations. I need to randomly choose starting locations with the ...
gallamine's user avatar
  • 875
7 votes
6 answers
14k views

Is Pythons random.randint statistically random?

So I'm testing an calculating the probabilities of certain dice rolls, for a game. The base case if that rolling one 10sided die. I did a million samples of this, and ended up with the following ...
Frames Catherine White's user avatar
7 votes
3 answers
9k views

Picking a random item based on probabilities

There's a similar question, I know, but it confused me, so I thought it easier to ask in my way. So I have an array of values, positive and negative. The higher they are, the more probability they ...
FizzBuzz's user avatar
  • 558
7 votes
3 answers
8k views

Choosing n numbers with fixed sum

In some code I want to choose n random numbers in [0,1) which sum to 1. I do so by choosing the numbers independently in [0,1) and normalizing them by dividing each one by the total sum: numbers = [...
Thomas Ahle's user avatar
  • 31.2k
7 votes
10 answers
11k views

JavaScript - How to randomly sample items without replacement?

JavaScript I've tried searching for something like this, but I am not able to find it. It's a simple idea: a. Take a random number between 0 to 10. b. Let's say the random number rolled is a 3. c....
seRgiOOOOOO's user avatar
7 votes
6 answers
5k views

Ruby: Using rand() in code but writing tests to verify probabilities

I have some code which delivers things based on weighted random. Things with more weight are more likely to be randomly chosen. Now being a good rubyist I of couse want to cover all this code with ...
Alex Wayne's user avatar
  • 183k
7 votes
3 answers
8k views

How to properly sample truncated distributions?

I am trying to learn how to sample truncated distributions. To begin with I decided to try a simple example I found here example I didn't really understand the division by the CDF, therefore I ...
Alexander Cska's user avatar

1
2 3 4 5
12