-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPsedoRand2Conditions.m
72 lines (50 loc) · 1.78 KB
/
PsedoRand2Conditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function [ Sequence ] = PsedoRand2Conditions( C0 , C1 , nodisplay )
%PSEDORAND2CONDITIONS Generates a sequences of 0 and 1 where there is no
%two 1 in a row
%
% C0 is the number of the 0 in the sequence
% C1 is the number of the 1 in the sequence
% nodisplay is 0 or 1 (flag)
%
% WARNING : the method is adapted for C0|C1 not too big (<500|250) and
% for C1/C0~0.5.
% CENIR-ICM , 2016
if nargin < 3
nodisplay = 0;
end
maxiteration = 100000;
L = C0 + C1; % length of the sequence
StreamRand = 0; % initialization of stram
exit_randomization = 0; % flag
iteration = 0; % counter
while ~exit_randomization
StreamRand(end+1) = round(rand); %#ok<AGROW> add a random value to the stream
if StreamRand(end-1) == 1 && StreamRand(end) == 1 % two 1 in a row
StreamRand(end) = 0; % change the new 1 into 0
end
if length(StreamRand) == L
for idx = 1:(length(StreamRand)-L)
window = StreamRand(idx:idx+L-1); % sliding window inside the stream
res = sum(window); % sum of 1
if res == C1 % "le compte est bon"
exit_randomization = 1;
end
end
elseif length(StreamRand) > L % enouth stream
iteration = iteration + 1;
if ~nodisplay
fprintf('iteration = %d \n',iteration)
end
window = StreamRand(iteration+1:iteration+L); % sliding window inside the stream
res = sum(window); % sum of 1
if res == C1 % "le compte est bon"
exit_randomization = 1;
end
end
if iteration == maxiteration % security
error('maxiteration rached')
end
end
Sequence = window;
end