dimanche 17 novembre 2019

How to use the PRNG linear congruential generator for rolling one dice

I have to design a linear congruential generator for rolling one dice. The general equation I use is defined by the recurrence relation:

enter image description here

where a,b en c are large and positive values. The result of the generator is obtained by dividing vector R by the largest number possible in R, (c-1).

I got two set of constants [a,b,c] and somehow, for the constants reported below in my code, it is possible to throw 0 with the dice. This does not happen all the times, but lets say 50% times when I run the code. Furthermore, I throw rarely 7's, which I try to prevent by the line 'Prevent throwns of 7'. Strangely, I do not have these problems when I use a = 1664525, b = 1013904223 and c = 2^32.

How is this possible and how can I solve this? Or does it have something to do with the constants?

function [out_2] = ThrowDie_2(N,F,a,b,c)
N = 5000; % amount of times the die is thrown
F = 6; % amount of faces of die
a = 3223; 
b = 3323;
c = 3486;
seed = now %Time of the system, the whole part of t corresponds to the date, 
        % and the fractional part corresponds to the time of day. Use
        % format longG to see the decimals. The time of the system will be
        % used as the initial seed for the LCG. 
out_int_2(1) = seed; %The initial value, seed that determines the sequence
    for i=2:N+1
        out_int_2(i) = mod(a.*out_int_2(i-1).*seed+b,c);
        out_2(i) = (out_int_2(i)/(c-1))*F;
        seed = out_2(i);  
    end
if out_2(2:N+1)<=6 ;  %Prevent throwns of 7
  out_2 = ceil(out_2(2:N+1));
else out_2 = round(out_2(2:N+1));
end 
end



Aucun commentaire:

Enregistrer un commentaire