## how does random() actually work?

The entire first chapter of Donald Knuth’s seminal work Seminumerical Algorithms is taken up with the subject of random number generation. I really don’t think an SO answer is going to come close to describing the issues involved. Read the book.

## How to test random numbers?

With most tests you can supply a large file of random numbers (integer or floating point) and run various tests on that sample file. DIEHARD worked that way, if I remember correctly and some others do, too. If you really want to see your generator fail, you could try using TestU01 by Pierre L’Ecuyer which … Read more

## Generate random numbers according to distributions

The standard random number generator you’ve got (rand() in C after a simple transformation, equivalents in many languages) is a fairly good approximation to a uniform distribution over the range [0,1]. If that’s what you need, you’re done. It’s also trivial to convert that to a random number generated over a somewhat larger integer range. … Read more

## How do I create a random String by sampling from alphanumeric characters?

As explained in the rand 0.5.0 docs, gen_ascii_chars is deprecated and you should use sample_iter(&Alphanumeric) instead. use rand::{distributions::Alphanumeric, Rng}; // 0.8 fn main() { let s: String = rand::thread_rng() .sample_iter(&Alphanumeric) .take(7) .map(char::from) .collect(); println!(“{}”, s); }

## random unit vector in multi-dimensional space

One simple trick is to select each dimension from a gaussian distribution, then normalize: from random import gauss def make_rand_vector(dims): vec = [gauss(0, 1) for i in range(dims)] mag = sum(x**2 for x in vec) ** .5 return [x/mag for x in vec] For example, if you want a 7-dimensional random vector, select 7 random … Read more

## How to properly seed random number generator

Each time you set the same seed, you get the same sequence. So of course if you’re setting the seed to the time in a fast loop, you’ll probably call it with the same seed many times. In your case, as you’re calling your randInt function until you have a different value, you’re waiting for … Read more

## how to generate integer random number in fortran 90 in the range [0,5]?

To support the answer by Alexander Vogt, I’ll generalize. The intrinsic random_number(u) returns a real number u (or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.] To have a discrete uniform distribution on the integers {n, n+1, …, m-1, m} carve the continuous … Read more

## Random order & pagination Elasticsearch

This should be considerably faster than both answers above and supports seeding: curl -XGET ‘localhost:9200/_search’ -d ‘{ “query”: { “function_score” : { “query” : { “match_all”: {} }, “random_score” : {} } } }’; See: https://github.com/elasticsearch/elasticsearch/issues/1170

## How to get truly random data, not random data fed into a PRNG seed like CSRNG’s do?

As you know, “truly random” means each of the bits is independent of everything else as well as uniformly distributed. However, this ideal is hard, if not impossible, to achieve in practice. In general, the closest way to get “truly random data” in practice is to gather hard-to-guess bits from nondeterministic sources, then condense those … Read more

## Lua math.random not working

You need to run math.randomseed() once before using math.random(), like this: math.randomseed(os.time()) One possible problem is that the first number may not be so “randomized” in some platforms. So a better solution is to pop some random number before using them for real: math.randomseed(os.time()) math.random(); math.random(); math.random() Reference: Lua Math Library