Questions tagged [random-generation]

The act of generating a sequence of numbers or symbols randomly, or (almost always) pseudo-randomly; i.e., with lack of any predictability or pattern.

Random generation, or random number generation is the use of mechanical or electronic devices or more usually algorithms to produce a sequence of numbers which lack any predictability or regularities, generally according to some particular probability distribution over the numbers or symbols, often equiprobably (when this makes sense). These values are used to approximate expectations, validate models, or to reproduce random experiments. Most random generators are pseudo-random in that they produce a deterministic sequence that can be repeated by using the "seed" the generators are initialised with.

Reference: http://en.wikipedia.org/wiki/Random_number_generation

713 questions
98
votes
8 answers

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

For a simulation study I have to generate random variables that show a predefined (population) correlation to an existing variable $Y$. I looked into the R packages copula and CDVine which can produce random multivariate distributions with a given…
Felix S
  • 4,432
  • 4
  • 26
  • 34
82
votes
7 answers

How to generate uniformly distributed points on the surface of the 3-d unit sphere?

I am wondering how to generate uniformly distributed points on the surface of the 3-d unit sphere? Also after generating those points, what is the best way to visualize and check whether they are truly uniform on the surface $x^2+y^2+z^2=1$?
Qiang Li
  • 1,145
  • 2
  • 9
  • 10
68
votes
8 answers

How to simulate data that satisfy specific constraints such as having specific mean and standard deviation?

This question is motivated by my question on meta-analysis. But I imagine that it would also be useful in teaching contexts where you want to create a dataset that exactly mirrors an existing published dataset. I know how to generate random data…
Jeromy Anglim
  • 42,044
  • 23
  • 146
  • 250
61
votes
11 answers

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

This is a question I found on Glassdoor: How does one generate 7 integers with equal probability using a coin that has a $\mathbb{Pr}(\text{Head}) = p\in(0,1)$? Basically, you have a coin that may or may not be fair, and this is the only…
Amazonian
  • 1,394
  • 1
  • 10
  • 19
60
votes
4 answers

How to generate correlated random numbers (given means, variances and degree of correlation)?

I'm sorry if this seems a bit too basic, but I guess I'm just looking to confirm understanding here. I get the sense I'd have to do this in two steps, and I've started trying to grok correlation matrices, but it's just starting to seem really…
43
votes
5 answers

Fake uniform random numbers: More evenly distributed than true uniform data

I'm looking for a way to generate random numbers that appear to be uniform distributed -- and every test will show them to be uniform -- except that they are more evenly distributed than true uniform data. The problem I have with the "true" uniform…
43
votes
10 answers

How to efficiently generate random positive-semidefinite correlation matrices?

I would like to be able to efficiently generate positive-semidefinite (PSD) correlation matrices. My method slows down dramatically as I increase the size of matrices to be generated. Could you suggest any efficient solutions? If you are aware of…
Eduardas
  • 2,239
  • 4
  • 23
  • 22
42
votes
3 answers

Random number-Set.seed(N) in R

I realize that one uses set.seed() in R for pseudo-random number generation. I also realize that using the same number, like set.seed(123) insures you can reproduce results. But what I don't get is what do the values themselves mean. I am playing…
mylesg
  • 613
  • 1
  • 5
  • 6
42
votes
8 answers

Approximate $e$ using Monte Carlo Simulation

I've been looking at Monte Carlo simulation recently, and have been using it to approximate constants such as $\pi$ (circle inside a rectangle, proportionate area). However, I'm unable to think of a corresponding method of approximating the value of…
35
votes
3 answers

Are the digits of $\pi$ statistically random?

Suppose you observe the sequence: 7, 9, 0, 5, 5, 5, 4, 8, 0, 6, 9, 5, 3, 8, 7, 8, 5, 4, 0, 0, 6, 6, 4, 5 , 3, 3, 7, 5, 9, 8, 1, 8, 6, 2, 8, 4, 6, 4, 1, 9, 9, 0, 5, 2, 2, 0, 4, 5, 2, 8 ... What statistically tests would you apply to determine if this…
Cam.Davidson.Pilon
  • 11,476
  • 5
  • 47
  • 75
35
votes
3 answers

What exactly is a seed in a random number generator?

I tried some usual google search etc. but most of the answers I find are either somewhat ambiguous or language/library specific such as Python or C++ stdlib.h etc. I am looking for a language agnostic, mathematical answer, not the specifics of a…
Della
  • 453
  • 1
  • 4
  • 6
35
votes
11 answers

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

I am generating 8 random bits (either a 0 or a 1) and concatenating them together to form an 8-bit number. A simple Python simulation yields a uniform distribution on the discrete set [0, 255]. I am trying to justify why this makes sense in my…
34
votes
2 answers

Am I creating bias by using the same random seed over and over?

In almost all of the analysis work that I've ever done I use: set.seed(42) It's an homage to Hitchhiker's Guide to the Galaxy. But I'm wondering if I'm creating bias by using the same seed over and over.
Brandon Bertelsen
  • 6,672
  • 9
  • 35
  • 46
34
votes
5 answers

How to sample from a discrete distribution?

Assume I have a distribution governing the possible outcome from a single random variable X. This is something like [0.1, 0.4, 0.2, 0.3] for X being a value of either 1, 2, 3, 4. Is it possible to sample from this distribution, i.e. generate pseudo…
33
votes
1 answer

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

I got a strange question when I was experimenting some convex optimizations. The question is: Suppose I randomly (say standard normal distribution) generate a $N \times N$ symmetric matrix, (for example, I generate upper triangular matrix, and fill…
Haitao Du
  • 32,885
  • 17
  • 118
  • 213
1
2 3
47 48