comparison m-toolbox/classes/@plist/getSetRandState.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % GETSETRANDSTATE gets or sets the random state of the MATLAB functions 'rand' and 'randn'
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: GETSETRANDSTATE gets or sets the random state of the MATLAB
5 % functions 'rand' and 'randn'. This function looks in the
6 % input plist for the key-word 'rand_state' (LTPDA toolbox
7 % less than 2.1) or 'rand_stream' (LTPDA toolbox greate equal
8 % than 2.1) to set the random state. If these key words
9 % doesn't exist in the plist then stores this function the
10 % random state in the plist.
11 %
12 % CALL: pl = getSetRandState(pl_in);
13 % pl = pl_in.getSetRandState();
14 % pl_in.getSetRandState();
15 %
16 % <a href="matlab:utils.helper.displayMethodInfo('plist', 'getSetRandState')">Parameters Description</a>
17 %
18 % VERSION: $Id: getSetRandState.m,v 1.10 2011/08/15 10:42:00 hewitson Exp $
19 %
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22 function varargout = getSetRandState(varargin)
23
24 %%% Check if this is a call for parameters
25 if utils.helper.isinfocall(varargin{:})
26 varargout{1} = getInfo(varargin{3});
27 return
28 end
29
30 % Collect all PLISTs
31 plin = utils.helper.collect_objects(varargin(:), 'plist');
32
33 % Decide on a deep copy or a modify
34 plout = copy(plin, nargout);
35
36 if numel(plout) ~= 1
37 error('### This method accepts only one input plist object.');
38 end
39
40 def_struct = struct('Type', '', ...
41 'NumStreams', [], ...
42 'StreamIndex', [], ...
43 'Substream', 1, ...
44 'Seed', 0, ...
45 'State', [], ...
46 'RandnAlg', '', ...
47 'Antithetic', false, ...
48 'FullPrecision', true);
49
50 rand_state = plout.find('rand_state');
51 rand_stream = plout.find('rand_stream');
52
53 %%% We have different behavior for the MATLAB version 2008b and less than
54 %%% version 2008b because version 2008b is working with random streams.
55
56 %%% MATLAB version 2008a and less
57 if verLessThan('MATLAB', '7.7')
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %%% MATLAB version 2008a and less %%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61
62 if ~isempty(rand_state)
63 %%% Reset random state %%%
64
65 %%% It might be that 'rand_state' contains the 'state' or the 'seed'
66 %%% of the MATLAB rand or randn methods.
67 try
68 randn('seed',rand_state);
69 rand('seed', rand_state);
70 catch
71 if numel(rand_state) > 2
72 rand('state', rand_state)
73 else
74 randn('state', rand_state)
75 end
76 end
77 plout.remove('rand_state');
78 plout.getSetRandState();
79
80 elseif ~isempty(rand_stream)
81 %%% Reset random state %%%
82
83 %%% Legacy mode
84 if numel(rand_stream) ~= 1
85 %%% Set that of 'randn'
86 randn('state', rand_stream(1).State)
87 %%% Set that of 'rand'
88 rand('state', rand_stream(2).State)
89 else
90 error('### I have no idea what I should do because the random state is stores as a random stream with MATLAB version 2008b or later.')
91 end
92
93 else
94 %%% Store random state %%%
95
96 stream = [def_struct def_struct];
97 %%% Store state of 'randn'
98 stream(1).Type = 'randn';
99 stream(1).State = randn('state');
100 %%% Store state of 'rand'
101 stream(2).Type = 'rand';
102 stream(2).State = rand('state');
103 plout.append('rand_stream', stream);
104 end
105
106 else
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 %%% MATLAB version 2008b and later %%%
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 def_stream = RandStream.getDefaultStream;
112
113 if ~isempty(rand_state)
114 %%% Reset random state %%%
115
116 %%% It might be that 'rand_state' contains the 'state' or the 'seed'
117 %%% of the MATLAB rand or randn methods.
118 try
119 randn('seed',rand_state);
120 rand('seed', rand_state);
121 catch
122 if numel(rand_state) > 2
123 rand('state', rand_state)
124 else
125 randn('state', rand_state)
126 end
127 end
128 plout.remove('rand_state');
129 plout.getSetRandState();
130
131 elseif ~isempty(rand_stream)
132 %%% Reset random state %%%
133
134 %%% Legacy mode
135 if numel(rand_stream) ~= 1
136
137 %%% Set that of 'randn'
138 randn('state', rand_stream(1).State)
139 %%% Set that of 'rand'
140 rand('state', rand_stream(2).State)
141 else
142 stream = RandStream(rand_stream.Type, 'Seed', rand_stream.Seed, 'RandnAlg', rand_stream.RandnAlg);
143 stream.State = uint32(rand_stream.State);
144 stream.Substream = rand_stream.Substream;
145 stream.Antithetic = rand_stream.Antithetic;
146 stream.FullPrecision = rand_stream.FullPrecision;
147 RandStream.setDefaultStream(stream);
148 end
149
150 else
151 %%% Store random state %%%
152
153 if strcmp(def_stream.Type, 'legacy')
154 %%%%%%%%%% Legacy Mode %%%%%%%%%%
155
156 stream = [def_struct def_struct];
157 %%% Store state of 'randn'
158 stream(1).Type = 'randn';
159 stream(1).State = randn('state');
160 %%% Store state of 'rand'
161 stream(2).Type = 'rand';
162 stream(2).State = rand('state');
163
164 else
165 %%%%%%%%%% Stream Mode %%%%%%%%%%
166 stream = def_struct;
167 stream.Type = def_stream.Type;
168 stream.NumStreams = def_stream.NumStreams;
169 stream.StreamIndex = def_stream.StreamIndex;
170 stream.Substream = def_stream.Substream;
171 stream.Seed = def_stream.Seed;
172 stream.State = double(def_stream.State);
173 stream.RandnAlg = def_stream.RandnAlg;
174 stream.Antithetic = def_stream.Antithetic;
175 stream.FullPrecision = def_stream.FullPrecision;
176 end
177 plout.pset('rand_stream', stream);
178 end
179
180
181 end
182
183 varargout{1} = plout;
184 end
185
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 %
188 % FUNCTION: getInfo
189 %
190 % DESCRIPTION: Get Info Object
191 %
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193
194 function ii = getInfo(varargin)
195 if nargin == 1 && strcmpi(varargin{1}, 'None')
196 sets = {};
197 pl = [];
198 else
199 sets = {'Default'};
200 pl = getDefaultPlist;
201 end
202 % Build info object
203 ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.internal, '$Id: getSetRandState.m,v 1.10 2011/08/15 10:42:00 hewitson Exp $', sets, pl);
204 ii.setArgsmin(1);
205 ii.setArgsmax(1);
206 end
207
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 %
210 % FUNCTION: getDefaultPlist
211 %
212 % DESCRIPTION: Get Default Plist
213 %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215
216 function plo = getDefaultPlist()
217 plo = plist();
218 end
219