Skip to content

Commit 12e0bbb

Browse files
authored
Version 1.0.
Initial upload of the Linear Congruential Generator for Uniform random numbers.
1 parent 35e837a commit 12e0bbb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

linearcongruential.m

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
%% Linear Congruential Generator for Uniform Random Deviates
2+
3+
clear all
4+
close all
5+
6+
% Parameters
7+
a = 1597 ;
8+
b = 51749 ;
9+
M = 244944 ;
10+
N0 = 40243 ; % Seed number
11+
12+
ndeviates = 1000 ; % Number of deviates required
13+
14+
% Algorithm
15+
A = zeros([1,ndeviates]) ; % Initialise zeros matrix for number of deviates
16+
A(1) = mod(a*N0 + b , M) ; % Start at the beginning
17+
for i = 2:ndeviates
18+
A(i+1) = mod(a*A(i) + b , M) ;
19+
end
20+
21+
U = A ./ M ; % Transform our numbers into standard uniform deviates
22+
23+
X = U(1:ndeviates-1) ; % This will be the U(i) plane
24+
Y = U(2:ndeviates) ; % This will be the U(i+1) plane
25+
26+
% Plot the figure to see how they lie
27+
% They could fall onto parallel lines:
28+
% e.g. When a = 1229; b=1; M = 2048
29+
% Or they could fall (relatively) well distributed:
30+
% e.g. When a = 1597; b = 51749; M = 244944
31+
figure(1)
32+
plot(X,Y,'r.')

0 commit comments

Comments
 (0)