Skip to content

Instantly share code, notes, and snippets.

@nikAizuddin
Last active April 12, 2019 02:46
Show Gist options
  • Select an option

  • Save nikAizuddin/634ae1caea57f0a0ee45 to your computer and use it in GitHub Desktop.

Select an option

Save nikAizuddin/634ae1caea57f0a0ee45 to your computer and use it in GitHub Desktop.
Lehmer's Algorithm using matlab
% Lehmer's algorithm
function [] = random()
m = 13; % modulus value
x = 0:0:m-1; % will store a sequence of random number
a = 7; % multiplier
i = 0; % general-purpose counter
c = 0; % non-negative integer
p = 1; % full-period multiplier counter
temp = 0; % temporary variable
% Initial seed
% WARNING! x(1) must not equal to 0 and not equal to m
% Make sure to set the x in this range: 0 < x(1) < m
x(1) = 1;
% Generate a sequence of random numbers
% Formula: x[i+1] = (a^i * x[1]) mod m
for i=1:1:m-1
x(i+1) = mod( a^i*x(1) ,m);
end
x
% Check if a is full-period multiplier
p = 1;
temp = a;
while temp ~= 1
p = p + 1;
temp = mod(a*temp, m);
end
if p == m - 1
fprintf('a is a full-period multiplier\n');
else
fprintf('a is NOT a full-period multiplier\n');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment