root/library/utia_legacy/ticket_12/randun.m

Revision 706, 1.4 kB (checked in by smidl, 14 years ago)

eol-native

  • Property svn:eol-style set to native
Line 
1function r=randun(m,n)
2%RANDUN generate sample from Uniform distribution
3%
4% r = randun
5% r = randun(m)
6% r = randun(m,n)
7%
8% r       : generated sample
9% SEED    : global variable SEED
10% m       : rows
11% n       : columns
12%
13% Design  : J. Andrysek
14% Project : BadDyr
15% Comments: used to generate exactly the same numbers in .m and .dll
16% versions
17%
18% Patched by Ludvik Tesar to output matrix instead of scalar
19 
20
21global SEED;
22if(isempty(SEED)) SEED = 1111111; end;
23%SEED = randn('seed');
24
25global RANDUN_COUNTER;
26if(isempty(RANDUN_COUNTER)) RANDUN_COUNTER = 0; end;
27
28
29if ~exist('m','var'); m = 1; end;
30if ~exist('n','var'); n = 1; end;
31
32r = zeros(m,n);
33
34for i=1:(m*n);
35
36% Ahrens linear congruential pseudo-random generator with A=663608941 B=0 M=2^32
37%    A      = 663608941;
38%    M      = 4294967296;
39%    SEED   = soucinek(A,SEED);
40%    r(i)   = SEED/M;
41% replaced by:
42
43% Park-Miller linear congruential pseudo-random generator with A=16807 B=0 M=2^31-1
44%     (it spans all 2^31-1 numbers and has good statistical properties)
45    A    = 16807;
46    M    = 2147483647;
47    SEED = mod(A*SEED,M);
48    r(i) = SEED/M;
49end;
50
51%randn('seed',SEED);
52
53RANDUN_COUNTER = RANDUN_COUNTER+m*n;
54
55function r=soucinek(a,b);
56z=2^16;
57ha(1)=floor(a/z);
58ha(2)=mod(a,z);
59
60hb(1)=floor(b/z);
61hb(2)=mod(b,z);
62
63c=ha(2)*hb(2);
64res(2)=mod(c,z);
65res(1)=mod(floor(c/z)+hb(2)*ha(1)+hb(1)*ha(2),z);
66
67r=res(1)*z+res(2);
68
Note: See TracBrowser for help on using the browser.