Skip to main content
63 votes
Accepted

Why is generating 8 random bits uniform on (0, 255)?

TL;DR: The sharp contrast between the bits and coins is that in the case of the coins, you're ignoring the order of the outcomes. HHHHTTTT is treated as the same as TTTTHHHH (both have 4 heads and 4 ...
Sycorax's user avatar
  • 92.6k
59 votes

Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p?

Flip the coin twice. If it lands HH or TT, ignore it and flip it twice again. Now, the coin has equal probability of coming up ...
NcAdams's user avatar
  • 661
58 votes

Generate a random variable with a defined correlation to an existing variable(s)

I will describe the most general possible solution. Solving the problem in this generality allows us to achieve a remarkably compact software implementation: just two short lines of ...
whuber's user avatar
  • 330k
58 votes
Accepted

Generating random numbers manually

If "manually" includes "mechanical" then you have many options available to you. To simulate a Bernoulli variable with probability half, we can toss a coin: $0$ for tails, $1$ for ...
Silverfish's user avatar
  • 23.8k
52 votes
Accepted

Generating correlated binomial random variables

Binomial variables are usually created by summing independent Bernoulli variables. Let's see whether we can start with a pair of correlated Bernoulli variables $(X,Y)$ and do the same thing. Suppose ...
whuber's user avatar
  • 330k
50 votes
Accepted

Finding a way to simulate random numbers for this distribution

There is a straightforward (and if I may add, elegant) solution to this exercise: since $1-F(x)$ appears like a product of two survival distributions: $$(1-F(x))=\exp\left\{-ax-\frac{b}{p+1}x^{p+1}\...
Xi'an's user avatar
  • 107k
49 votes

Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p?

Assume that $p \in (0,1)$. Step 1:. Toss the coin 5 times. If the outcome is $(H, H, H, T, T)$, return $1$ and stop. $(H, H, T, T, H)$, return $2$ and stop. $(H, T, T, H, H)$, return $3$ and ...
Dilip Sarwate's user avatar
47 votes

Generating random numbers manually

If you can get access to a very precise clock, you can extract the decimal part of the current time and turn it into a uniform, from which you can derive a normal simulation by the Box-Müller ...
Xi'an's user avatar
  • 107k
45 votes
Accepted

Who is Gail Gasram?

It looks like "Gail Gasram" is "Marsaglia G" (George Marsaglia's surname and first initial) spelled backwards.
alivenets's user avatar
  • 516
44 votes
Accepted

What exactly is a seed in a random number generator?

Most pseudo-random number generators (PRNGs) are build on algorithms involving some kind of recursive method starting from a base value that is determined by an input called the "seed". The default ...
Ben's user avatar
  • 130k
43 votes
Accepted

If I generate a random symmetric matrix, what's the chance it is positive definite?

If your matrices are drawn from standard-normal iid entries, the probability of being positive-definite is approximately $p_N\approx 3^{-N^2/4}$, so for example if $N=5$, the chance is 1/1000, and ...
Alex R.'s user avatar
  • 14k
42 votes
Accepted

How to generate random integers between 1 and 4 that have a specific mean?

I agree with X'ian that the problem is under-specified. However, there is an elegant, scalable, efficient, effective, and versatile solution worth considering. Because the product of the sample mean ...
whuber's user avatar
  • 330k
38 votes

Why is the term "Monte Carlo simulation" used instead of "Random simulation"?

Nicholas Metropolis claimed in 1987 that It was at that time that I suggested an obvious name for the statistical method - a suggestion not unrelated to the fact that Stan[islaw ...
Stephan Kolassa's user avatar
33 votes
Accepted

How many numbers can I generate and be 90% sure that there are no duplicates?

The approximation given in the Wikipedia article (also mentioned in the paper by Diaconis & Mosteller (1989)) works well here. Suppose $N$ 4-digit numbers are drawn with replacement from a pool of ...
COOLSerdash's user avatar
31 votes
Accepted

R: Problem with runif: generated number repeats (more often than expected) after less than 100 000 steps

The documentation of R on random number generation has a few sentences at its end, that confirm your expectation of 32-bit integers being used and might explain what you are observing: Do not rely ...
L_W's user avatar
  • 451
29 votes

Would it be possible to generate data from real data in medical research?

No, this is not good practice. "Oversampling" the data you have makes your model believe it has more real data than it actually has, and makes it underestimate the probability of actual data ...
Stephan Kolassa's user avatar
28 votes
Accepted

How to sample from Cantor distribution?

Easy: sample from a Uniform$(0,1)$ distribution and recode from binary to ternary, interpreting each "1" as a "2". (This is the inverse probability transform approach: it does indeed invert the CDF!) ...
whuber's user avatar
  • 330k
26 votes
Accepted

Algorithm for sampling fixed number of samples from a finite population

Yes. Collect the first $k$ items encountered into the cache. At steps $j=k+1, \ldots, n,$ place item $j$ in the cache with probability $k/j,$ in which case you will remove one of the existing items ...
whuber's user avatar
  • 330k
24 votes
Accepted

How do I sample from a discrete (categorical) distribution in log space?

It is possible to sample from categorical distribution given log-probabilities without leaving log space using the Gumbel-max trick. The idea is that if you are given unnormalized log-probabilities $\...
Tim's user avatar
  • 140k
23 votes

Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p?

Generalizing the case described by Dilip Sarwate Some of the methods described in the other answers use a scheme in which you throw a sequence of $n$ coins in a 'turn' and depending on the result you ...
Sextus Empiricus's user avatar
22 votes

@whuber 's generation of a random variable with fixed covariance structure

As a point for further elaboration, here is the explanation in the thread you reference: If it's mathematically possible, [this method] will find an $X_{Y_1,Y_2,\ldots,Y_k;\rho_1,\rho_2,\ldots,\...
whuber's user avatar
  • 330k
21 votes

What exactly is a seed in a random number generator?

First, there is no true randomness in today's computer-generated "random numbers." All pseudorandom generators use deterministic methods. (Possibly, quantum computers will change that.) The ...
BruceET's user avatar
  • 57.1k
21 votes

Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p?

Divide a box into seven equal-area regions, each labeled with an integer. Throw the coin into the box in such a way that it has equal probability of landing in each region. This is only half in jest -...
Sycorax's user avatar
  • 92.6k
20 votes

Generating Multivariate Uniform Distribution in R

It depends a little bit on the terminology, but usually multivariate uniform refers to a distribution where every point in $[a,b]^d$ is equally likely. Hence, the dimensions are independent, and you ...
Sam's user avatar
  • 838
20 votes

Who is Gail Gasram?

Diehard Code After some extensive digging, it appears that Gail Gasram participated in developing Diehard code, which represented a suite of programs for testing random number generators. Furthermore,...
PsychometStats's user avatar
19 votes

Will this introduce bias into what should be random numbers?

Let's count and see. By construction of the file, all 4-bit strings are equally likely. There are 16 such strings. Here they are: ...
Kodiologist's user avatar
  • 20.4k
18 votes
Accepted

What is meant by "Laplace noise"?

You are correct, adding Laplace noise means that to your variable $X$ you add variable $Y$ that follows Laplace distribution. There are multiple reasons why it is called noise. First, think of signal ...
Tim's user avatar
  • 140k
18 votes
Accepted

How to generate a $\pm 1$ sequence with mean $0.05$?

Your desired mean is given by equation: $\frac{N\cdot p - N \cdot (1-p)}{N} = .05$ from which follows that the probability of the 1s should be ...
Sergey Bushmanov's user avatar
18 votes
Accepted

Sampling from $x^2\phi(x)$?

Some guesswork suggest that $X$ perhaps can be simulated by a suitable power-transformation of a Gamma random variable $Y$ multiplied by a random sign to make the resulting density symmetric about ...
Jarle Tufto's user avatar
  • 11.5k
17 votes

Why is generating 8 random bits uniform on (0, 255)?

why does a sequence of 8 zeroes or 8 ones seem to be equally as likely as a sequence of 4 and 4, or 5 and 3, etc The aparent paradox can be summarized in two propositions, that might seem ...
leonbloy's user avatar
  • 1,468

Only top scored, non community-wiki answers of a minimum length are eligible