prng

C++11 Generating random numbers from frequently changing range

有些话、适合烂在心里 提交于 2019-12-01 17:05:58
Q: How do I generate (many) uniformly distributed integers from a-priory unknown ranges? What is the prefered way in terms of performance (milions of generated numbers)? Context: In my app I have to generate many pseudo random numbers in many places. I use singleton pattern for the generator to maintain reproducibility of the app's run. Distribution is always uniform in my case, but the problem is that there are far too many possible ranges to pre-made the distribution object in C++11 style. What I tried: There are two obvious solutions to this, first is to have one-time distribution objects

C++11 Generating random numbers from frequently changing range

主宰稳场 提交于 2019-12-01 16:52:08
问题 Q: How do I generate (many) uniformly distributed integers from a-priory unknown ranges? What is the prefered way in terms of performance (milions of generated numbers)? Context: In my app I have to generate many pseudo random numbers in many places. I use singleton pattern for the generator to maintain reproducibility of the app's run. Distribution is always uniform in my case, but the problem is that there are far too many possible ranges to pre-made the distribution object in C++11 style.

JavaScript pseudo-random sequence generator

若如初见. 提交于 2019-12-01 08:07:09
I need to generate a deterministic (i.e. repeatable) sequence of pseudo-random numbers given an initial seed and select the nth item from that sequence. If JavaScript's random function was seedable, I could just do: function randomNth(seed, seq) { var r; Math.randomSeed(seed); for (var i = 0; i++ < seq; i++) { r = Math.random(); } return r; } However, it's not, and alternative, seedable PRNGs look to be a little slow; asking for the 250th number would be expensive. I think a hash is what I want here, perhaps something like md5(seed + seq) % max but JavaScript doesn't have md5() and if I'm

JavaScript pseudo-random sequence generator

北战南征 提交于 2019-12-01 07:48:43
问题 I need to generate a deterministic (i.e. repeatable) sequence of pseudo-random numbers given an initial seed and select the nth item from that sequence. If JavaScript's random function was seedable, I could just do: function randomNth(seed, seq) { var r; Math.randomSeed(seed); for (var i = 0; i++ < seq; i++) { r = Math.random(); } return r; } However, it's not, and alternative, seedable PRNGs look to be a little slow; asking for the 250th number would be expensive. I think a hash is what I

Default seed PRNG in Java

亡梦爱人 提交于 2019-12-01 07:16:51
I was wondering what the default seed for the PRNG* behind Math.random() in Java is. From what I understand the one in C is based upon the system clock. So is it similar in Java? Also, is the seed changed everytime Math.random() is called? *PRNG = Pseudo Random Number Generator If you Read The Fine Manual it tells you When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. Following up with java

Default seed PRNG in Java

半世苍凉 提交于 2019-12-01 05:29:04
问题 I was wondering what the default seed for the PRNG* behind Math.random() in Java is. From what I understand the one in C is based upon the system clock. So is it similar in Java? Also, is the seed changed everytime Math.random() is called? *PRNG = Pseudo Random Number Generator 回答1: If you Read The Fine Manual it tells you When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number

Generating a random double between a range of values

半腔热情 提交于 2019-12-01 04:37:08
问题 Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving me the same values but with a small change in the decimal field. ex : 27.xxx. Heres my code, any help would be appreciated. #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand( time(NULL) ); double r = (68.556*rand()/RAND_MAX - 32.768); cout << r << endl; return 0; } 回答1: I should mention if you're using a C++11 compiler, you can use something like this

CURAND Library - Compiling Error - Undefined reference to functions

a 夏天 提交于 2019-12-01 03:03:12
问题 I have the following code which I am trying to compile using nvcc. Code: #include <stdio.h> #include <stdlib.h> #include <cuda.h> #include <curand.h> int main(void) { size_t n = 100; size_t i; int *hostData; unsigned int *devData; hostData = (int *)calloc(n, sizeof(int)); curandGenerator_t gen; curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MRG32K3A); curandSetPseudoRandomGeneratorSeed(gen, 12345); cudaMalloc((void **)&devData, n * sizeof(int)); curandGenerate(gen, devData, n); cudaMemcpy

Generate Array of Numbers that fit to a Probability Distribution in Ruby?

谁说我不能喝 提交于 2019-11-30 16:43:14
Say I have 100 records, and I want to mock out the created_at date so it fits on some curve. Is there a library to do that, or what formula could I use? I think this is along the same track: Generate Random Numbers with Probabilistic Distribution I don't know much about how they are classified in mathematics, but I'm looking at things like: bell curve logarithmic (typical biology/evolution) curve? ... Just looking for some formulas in code so I can say this: Given 100 records, a timespan of 1.week , and an interval of 12.hours set created_at for each record such that it fits, roughly, to curve

Picking A, C and M for Linear congruential generator

风流意气都作罢 提交于 2019-11-30 13:44:29
问题 I am looking to implement a simple pseudorandom number generator (PRNG) that has a specified period and guaranteed no collisions for the duration of that period. After doing some research I came across the very famous LCG which is perfect. The problem is, I am having trouble understanding how to properly configure it. Here is my current implementation: function LCG (state) { var a = ?; var c = ?; var m = ?; return (a * state + c) % m; } It says that in order to have a full period for all seed