# Tag Info

## Hot answers tagged random-number-generation

13

To the best of my knowledge, Numpy does not support independent streams. Indeed, getting independent streams from the Mersenne Twister (Pythons RNG) is notoriously difficult although it can be done. Consider using the RandomGen package. It is fully compatible with Numpy, and provides you with the PCG64 generator, supporting up to $2^{63}$ independent ...

12

If you constrain yourself to full cycle LCG PRNGs then the answer is easy, by definition it's simply $m$. To find the period of a non full cycle LCG PRNG for a given seed you just need to count the number of iterations of the PRNG until it generates the seed value once more. From the referenced wikipedia page: Period length The period of a general ...

10

See the paper How to crack a Linear Congruential Generator, Haldir ("Reverse Engineering Team", Dec. 2004): In this paper I will present a method which will solve all values of the LCG including the modulus with six or more consecutive numbers of PRNG output. The paper includes "proof of concept" source code written in C, using Victor Shoup's NTL for ...

9

The trick is to interleave each process's LCG stream: for $p$ processes, we modify the LCG $$x_{n+1} := a x_n + c\;\;\; (\bmod m),$$ to be $$x_{n+p} := A_p x_n + C_p\;\;\; (\bmod m),$$ where $A_p$ and $C_p$ effectively step forward $p$ steps. We can quickly derive them by expanding the original LCG step: $$x_{n+2} = a (a x_n + c) + c\;\;\; (\bmod m)... 7 Cython makes code faster by removing the type ambiguity. Since random.py is a pure python module, you can just copy it and add the types to the functions you need. Then cython can optimize the dynamic overhead away. 7 Your proposal appears to make sense but what would be important to ask first is what probability distribution you want on those numbers. The set of numbers with only certain digits is rather peculiar as it is specific to the base-10 number system you choose. It would be interesting to hear what you intend to do with those random numbers! 7 Like you say, using the Mersenne Twister for parallel computations is almost always done incorrectly, as the correct method is tricky to implement. By far the easiest and best answer would be to move away from the Mersenne Twister entirely, and use something like the PCG family, which provides multiple streams out of the box. The Mersenne Twister is known ... 7 A little playing with the sequence of numbers generated by the C code shows that the sequence is z_{i+1}=5z_{i}+273 \mod 2^{16} This is a linear congruential generator (LCG). It's easy to show that this LCG has full period (See theorem 7.1 in Law's Simulation Modeling and Analysis, 5th ed. and check the three conditions.) I can't find the generator ... 6 If I understood your question correctly: Subdivide the interval [0,1] in N segments, each having a width proportional to your probabilities p_i (where \sum_{i=1}^N p_i = 1). Use a pseudo-random-number-generator using the uniform probability density distribution on [0,1] to generate the number x. Determine in which segment x is located; this ... 6 To the best of my knowledge, no, but maybe other people here know the field more intimately. My knowledge comes primarily from developing Monte Carlo codes in physics. Knuth, in volume 2 of his Art of Computer Programming, states that Metropolis, using the middle-square method on 20 bits, found 13 cycles to which the method would always degenerate, the ... 6 I suspect the reason why generating the random numbers on the fly is slower for you is due to the rather large state of the Mersenne Twister. Switching to something like the PCG or XorShift+ random number generator would have several advantages for you: Higher quality of randomness (Mersenne Twister fails several tests for randomness) Smaller state, so ... 5 The initial velocities are drawn from a Gaussian distribution with variance$$\sigma_i^2=\frac{k_{\textrm{B}}T}{m_i}, where $k_{\textrm{B}}$ denotes Boltzmann's constant, $T$ is the temperature and $m_i$ is the mass of the $i^{\textrm{th}}$ particle. Thus, the problem boils down to generate random numbers from a gaussian distribution using uniformly ...

5

You definitely want to derandomize your program during development. Otherwise you will not be able to debug it since problems are not reproducible. At the same time, once you know the algorithm is working, you need to run it for multiple seeds or with different random number generators to ensure that your results (such as ensemble averages, standard ...

5

Following aterrel's suggestion, you could use pyximport to automatically compile the random module: import pyximport pyximport.install(pyimport=True) import random However, this still will not make it as fast as it would be if you declared static types for the variables in Cython.

5

It is not a problem if one processor generates a number that has already appeared on a different processor. It would, however, be a problem if the two generated whole sequences that are similar. This is unlikely, if you start with different random seeds, since the number of numbers you get from a RNG before it starts to repeat itself (the cycle length) is so ...

5

A random number generator will give you random numbers that you can tweak to be between zero and 2^n and consequently it will allow you to sample random locations in your interval. These numbers may, in principle, repeat themselves -- so they are not a traversal, but that may not matter for the following reason: If $n$ is large (in the thousands), you will ...

4

I'd imagine that most users of random number generators are ultimately interested in floating-point values. This is why the Double precision SIMD-oriented Fast Mersenne Twister (dSFMT) exists. However, there is newer C code for the WELL RNG that returns unsigned long values. Looking at the code, it appears that the earlier version was casting unsigned long ...

4

As noted in the comments by Kirill, the y-axes of the two plots are very different. And if they are rescaled accordingly, the boxes will certainly look very similar, if not identical. Therefore, it is very reasonable to conclude that the raw simulation result in data.dat coming from your C++ code is correct, no matter what seed has been used for random-...

3

Testing whether or not the mean is correct, or even if the histogram of your generated random variants "looks" like a certain distribution is not sufficient. Stick with much more rigorous test suites such as TestU01 or Diehard. Also, you really only have TWO random numbers in each row, because of the constraint that they sum to 1. This requires more ...

3

I think the book Numerical Recipes--The Art of Scientific Computing,William H et al talks about each PRNG's strengths and weaknesses in chapter 7. If you want to test different PRNGs by yourself, you can try the TestU01 suite, which is developed by L'Ecuyer and Richard Simard, this suite contains 3 predefined batteries(SmallCrush, Crush, and BigCrush), ...

3

I believe one of the Art of Computer Programming books by Knuth is devoted exclusively to random numbers. It's probably not up to date, but it would be the place where I'd start reading.

2

I wanted to chime in to say that, I've recently gone through this process with a simulation and I should note that using Fortuna is not out of the question if it is really necessary. In our case, we were concerned that MT's entropy wasn't high enough which would translate in our simulation to a bias. So for our simulation we used Fortuna pulling about 65 ...

2

Did you have a look at dieharder? Dieharder is a random number generator (rng) testing suite. It is intended to test generators, not files of possibly random numbers as the latter is a fallacious view of what it means to be random. Is the number 7 random? If it is generated by a random process, it might be. If it is made up to serve the purpose of some ...

2

This answer is late in coming, but you should have a look at SPRNG. It's specifically designed for scalability in parallel and supports a handful of types of PRNGs.

2

A simple idea for spreading a typical sequential RNG over a decent number of threads is to have a single thread advance the seed as fast as possible and send only every thousandth or so seed out to memory. Then have each of your other threads pick up one of these spaced out reference seeds and process the 1000 values in that block, i.e. regenerate again the ...

2

I'm not sure if these were added recently but it seems like there are now easy ways to generate random numbers quickly without too much overhead. From this article about Monte Carlo simulations in cython we can do from libc.stdlib cimport rand, RAND_MAX r = 1 + int(rand()/(RAND_MAX*6.0)) # random integer 1,...,6 As far as I understand you don't need to do ...

2

If you want to analyze the sensitivity of your outcome on changes in the input space, you would want something like Dakota or SUSA (which is described in this paper). These codes allow you to run your simulation as a black box a number of times while sampling the parameters from the probability distribution you assign to them. The output is a statistical ...

2

You could try using something like a very basic Relational database. You could label every output file with a separate key, e.g. a sequential number, and then maintain a separate file which, in every row, contains the keys and the parameters used. If you're processing the data automatically, you will have to use one level of indirection, but that still ...

2

If the data object you are trying to map is large enough, then just taking bit patterns is probably enough. For example, you might simply xor all the bytes of your object, giving you a number between 0 and 255 which you can then map onto the reals between 0 and 1.

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