I have to write pseudo-random generator on assembler without any float/double operations and functions, like sin/cos, sqrt e.t.c. So I can't use general methods to do that. Also I have limit for random-numbers: 00-0F. How can I do this?
As I understood, I need to generate uniform-number at first. I did it like this: x = (13 * x + 7) % 16;
(but it has a problem - it's the unifromest distribution ever. If it generated 15 numbers and I know all of them, I can say 16th with 100% probability, because where is no repetition in period which is 16 (module) ).
And after that, I need to regenerate those numbers to gaussian. I found this solution in the internet, but it doesn't work.
for (i = 0; i < N; ++i) // N - amount of randomized numbers
{
++gx[x = (a * x + c) % m]; //gx - histogram of x
xm[i] = x; // xm - massive of randomized numbers in uniform
y = 0;
for (j = 0; j < i + 1; ++j)
{
y += xm[j] * n - j; // n - primitive number. I choose 13
}
y = y / (i + 1);
y %= m;
ym[i] = y; // ym - massive of randomized numbers in gaussian
++gy[y]; //gy - histogram of y
}
What should I do with it? (I know nothing about probability theory)
I get this output of gx and gy:
Uniform
0 4 ****
1 4 ****
2 4 ****
3 4 ****
4 4 ****
5 4 ****
6 4 ****
7 4 ****
8 4 ****
9 4 ****
10 4 ****
11 4 ****
12 4 ****
13 4 ****
14 4 ****
15 4 ****
Normal
0 2 **
1 3 ***
2 8 ********
3 4 ****
4 10 **********
5 4 ****
6 1 *
7 2 **
8 1 *
9 3 ***
10 8 ********
11 4 ****
12 5 *****
13 6 ******
14 1 *
15 2 **
Use rand to generate 1000 random numbers from the uniform distribution on the interval (0,1). rng('default') % For reproducibility u = rand(1000,1); The inversion method relies on the principle that continuous cumulative distribution functions (cdfs) range uniformly over the open interval (0,1).
Using the inverse function is how we will get our set of normally distributed random values. We will use the RAND() function to generate a random value between 0 and 1 on our Y-axis and then get the inverse of it with the NORM. INV function which will result in our random normal value on the X-axis.
Description. r = normrnd( mu , sigma ) generates a random number from the normal distribution with mean parameter mu and standard deviation parameter sigma . r = normrnd( mu , sigma , sz1,...,szN ) generates an array of normal random numbers, where sz1,...,szN indicates the size of each dimension.
Use a linear feedback shift register. All integer calculations, and you can peel off individual bytes to return your random number.
That page is just a particularly easy to approach example. There is lots of stuff available that talks about using LFSRs for generating pseudorandom numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With