File: ClockRandSeed.m

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (69 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (3)
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
function [seed,whichGen] = ClockRandSeed(seed,whichGen)
% [seed,whichGen] = ClockRandSeed([seed],[whichGen])
%
% ClockRandSeed seeds the random number from the time-of-day clock. If seed
% is passed this is used instead.
% 
% The multiplier 1e6 in fix(1e6*sum(clock)) is bigger than the 100
% suggested by Mathworks (see help RAND).  They suggest 100*sum(clock), but
% this would only change the seed once every 1/100 sec, which might
% correspond to many iterations of a loop. With the bigger  multiplier it's
% pretty sure (depending only on clock grain) that two successive calls
% will generate different seeds.
% 
% Also unlike the Mathworks suggestion (see help RAND), we call FIX. This
% has no effect on RAND and RANDN, but makes it easier to correctly print
% the seed to a file. When a random seed generates an interesting result
% one would like to be able to recreate the conditions that generated the
% run. If the seed 1e6*sum(clock) is printed to a file, '%.0f' will often
% round it differently than RAND would.
%
% Note in that in Matlab 5 there are two random number generators, the
% Matlab 5 generator and the Matlab 4 generator.  (Matlab 4
% has only the version 4 generator.)  ClockRandSeed checks what version of
% Matlab you're running and uses the version 5 generator if it can.  The
% string whichGen contains 'seed' for the version 4 generator and 'state'
% for the version 5 generator.
%
% See also: rand, randn.

% 10/15/93	dhb	Made this a function.
% 4/11/94	dhb	Added randn seeding.
% 8/9/97	dgp	Update for more reliable seed.
% 	        dgp Gutted it to make version-dependence explicit.
% 8/9/97    dhb Ungutted it to hide version-dependence.
% 8/13/97   dhb Optional return of which generator used and passing of seed/generator.
% 7/24/04   awi Cosmetic.
% 1/29/05   dgp Cosmetic.


if (nargin == 0)
	seed = fix(1e6*sum(clock));
	if sscanf(version,'%f',1)<5
		whichGen = 'seed';
	else
		whichGen = 'state';
	end
elseif (nargin == 1)
	if (isempty(seed))
		seed = fix(1e6*sum(clock));
	end
	if sscanf(version,'%f',1)<5
		whichGen = 'seed';
	else
		whichGen = 'state';
	end
else
	if (isempty(seed))
		seed = fix(1e6*sum(clock));
	end
	if (isempty(whichGen))
		if sscanf(version,'%f',1)<5
			whichGen = 'seed';
		else
			whichGen = 'state';
		end
	end
end

rand(whichGen,seed);randn(whichGen,seed);