-
Notifications
You must be signed in to change notification settings - Fork 1
/
i4_uniform.m
66 lines (54 loc) · 1.38 KB
/
i4_uniform.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
function [ c, seed ] = i4_uniform ( a, b, seed )
%*****************************************************************************80
%
%% I4_UNIFORM returns a scaled pseudorandom I4.
%
% Discussion:
%
% The pseudorandom number will be scaled to be uniformly distributed
% between A and B.
%
% Parameters:
%
% Input, integer A, B, the minimum and maximum acceptable values.
%
% Input, integer SEED, a seed for the random number generator.
%
% Output, integer C, the randomly chosen integer.
%
% Output, integer SEED, the updated seed.
%
i4_huge = 2147483647;
if ( seed == 0 )
fprintf ( 1, '\n' );
fprintf ( 1, 'I4_UNIFORM - Fatal error!\n' );
fprintf ( 1, ' Input SEED = 0!\n' );
error ( 'I4_UNIFORM - Fatal error!' );
end
seed = floor ( seed );
a = round ( a );
b = round ( b );
seed = mod ( seed, i4_huge );
if ( seed < 0 )
seed = seed + i4_huge;
end
k = floor ( seed / 127773 );
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
seed = seed + i4_huge;
end
r = seed * 4.656612875E-10;
%
% Scale R to lie between A-0.5 and B+0.5.
%
r = ( 1.0 - r ) * ( min ( a, b ) - 0.5 ) ...
+ r * ( max ( a, b ) + 0.5 );
%
% Use rounding to convert R to an integer between A and B.
%
value = round ( r );
value = max ( value, min ( a, b ) );
value = min ( value, max ( a, b ) );
c = value;
return
end