help coin_toss coin toss experiment [results, ave_value] = coin_toss (ntrials); 1. rand returns a number from a pseudo-random sequence based on a uniform distribution 2. coin toss (two outcomes) simulation: let H be the event that rand is in (0, 0.5), let T be the event that rand is in (0.5, 1.0) 3. represent H by 1 and T by 0 [results, ave_value] = coin_toss (10); ave_value(end) ans = 0.4000 [results, ave_value] = coin_toss (100); ave_value(end) ans = 0.4400 [results, ave_value] = coin_toss (100); ave_value(end) ans = 0.4400 [results, ave_value] = coin_toss (100); ave_value(end) ans = 0.4700 [results, ave_value] = coin_toss (200); ave_value(end) ans = 0.5100 [results, ave_value] = coin_toss (200); ave_value(end) ans = 0.4950 [results, ave_value] = coin_toss (200); ave_value(end) ans = 0.5450 [results, ave_value] = coin_toss (200); [results, ave_value] = coin_toss (200); [results, ave_value] = coin_toss (200); [results, ave_value] = coin_toss (200); ave_value(end) ans = 0.4300 [results, ave_value] = coin_toss (1000); ave_value(end) ans = 0.5140 [results, ave_value] = coin_toss (1000); ave_value(end) ans = 0.5030 [results, ave_value] = coin_toss (1000); ave_value(end) ans = 0.5000 [results, ave_value] = coin_toss (1000); ave_value(end) ans = 0.4630 s = RandStream.create('mt19937ar','seed',1); help RandStream RandStream Random number stream. (Pseudo)random numbers in MATLAB come from one or more random number streams. The simplest way to generate arrays of random numbers is to use RAND, RANDI, or RANDN. These functions all draw values from the same stream of uniform pseudorandom numbers, known as the global random number stream. You can create other streams that act separately from the current global stream, and you can use their RAND, RANDI, or RANDN methods to generate arrays of random numbers. You can also designate any random number stream you create as the global stream. To create a single random number stream, use the RandStream constructor. To create multiple independent random number streams, use RandStream.CREATE. The RNG function provides a simple interface to create a new global stream. STREAM = RandStream.GETGLOBALSTREAM returns the current global random number stream, i.e., the one that the RAND, RANDI, and RANDN functions currently draw values from. PREVSTREAM = RandStream.SETGLOBALSTREAM(STREAM) sets STREAM as the current global random number stream, i.e., designates it as the stream that the RAND, RANDI, and RANDN functions will draw values from. PREVSTREAM is the stream that was previously designated as the global random number stream. A random number stream S has properties that control its behavior. Access or assign to a property using P = S.Property or S.Property = P. RandStream properties: Type - (Read-only) identifies the type of generator algorithm used by the stream. Seed - (Read-only) the seed value used to create the stream. NumStreams - (Read-only) the number of streams created at the same time as the stream. StreamIndex - (Read-only) the stream's index among the group of streams in which it was created. State - the internal state of the generator. You should not depend on the format of this property, or attempt to improvise a property value. The value you assign to S.State must be a value read from S.State previously. Use RESET to return a stream to a predictable state without having previously read from the State property. Substream - the index of the substream to which the stream is currently set. The default is 1. Multiple substreams are not supported by all generator types. NormalTransform - the transformation algorithm that RANDN(S, ...) uses to generate normal pseudorandom values from uniform values. The property value is one of 'Ziggurat' (the default), 'Polar', or 'Inversion'. Antithetic - a logical value indicating whether S generates antithetic uniform pseudorandom values, that is, the usual values subtracted from 1. The default is false. FullPrecision - a logical value indicating whether S generates values using its full precision. Some generators are able to create pseudorandom values faster, but with fewer random bits, if FullPrecision is false. The default is true. The sequence of pseudorandom numbers produced by a random number stream S is determined by the internal state of its random number generator. Saving and restoring the generator's internal state via the 'State' property allows you to reproduce output. Examples: Create a single stream and designate it as the current global stream: s = RandStream('mt19937ar','Seed',1) RandStream.setGlobalStream(s); Create three independent streams: [s1,s2,s3] = RandStream.create('mrg32k3a','NumStreams',3); r1 = rand(s1,100000,1); r2 = rand(s2,100000,1); r3 = rand(s3,100000,1); corrcoef([r1,r2,r3]) Create only one stream from a set of three independent streams, and designate it as the current global stream: s2 = RandStream.create('mrg32k3a','NumStreams',3,'StreamIndices',2) RandStream.setGlobalStream(s2); Reset the global random number stream that underlies RAND, RANDI, and RANDN back to its beginning, to reproduce previous results: stream = RandStream.getGlobalStream; reset(stream); Save and restore the current global stream's state to reproduce the output of RAND: stream = RandStream.getGlobalStream; savedState = stream.State; u1 = rand(1,5) stream.State = savedState; u2 = rand(1,5) % u2 contains exactly the same values as u1 Reset the global random number stream to its "factory default" initial settings. This causes RAND, RANDI, and RANDN to start over, as if in a new MATLAB session. s = RandStream('mt19937ar','Seed',0) RandStream.setGlobalStream(s); Reinitialize the global random number stream using a seed based on the current time. This causes RAND, RANDI, and RANDN to return different values in different MATLAB sessions. NOTE: It is usually not desirable to do this more than once per MATLAB session. s = RandStream('mt19937ar','Seed','shuffle') RandStream.setGlobalStream(s); Change the transformation algorithm that RANDN uses to create normal pseudorandom values from uniform values. Note that this does not replace or reset the global stream. stream = RandStream.getGlobalStream; stream.NormalTransform = 'inversion' RandStream methods: RandStream/RandStream - Create a random number stream. create - Create multiple independent random number streams. list - List available random number generator algorithms. getGlobalStream - Get the current global random number stream. setGlobalStream - Replace the global random number stream. reset - Reset a stream to its initial internal state. rand - Pseudorandom numbers from a uniform distribution. randn - Pseudorandom numbers from a standard normal distribution. randi - Pseudorandom integers from a uniform discrete distribution. randperm - Random permutation. See also rng, randfun/rand, randfun/randn, randfun/randi. Reference page in Help browser doc RandStream RandStream.setGlobalStream(s); rand ans = 0.4170 rand ans = 0.7203 s = RandStream.create('mt19937ar','seed',1); RandStream.setGlobalStream(s); rand ans = 0.4170 rand ans = 0.7203 [results, ave_value, rel_unc] = coin_toss (10); rel_unc(end) ans = 0.3969 [results, ave_value, rel_unc] = coin_toss (100); rel_unc(end) ans = 0.4898 % I had an error in my code, which I fixed; corrected results appear below [results, ave_value, rel_unc] = coin_toss (10); rel_unc(end) ans = 0.3691 [results, ave_value, rel_unc] = coin_toss (100); rel_unc(end) ans = 0.1044 [results, ave_value, rel_unc] = coin_toss (200); rel_unc(end) ans = 0.0789 [results, ave_value, rel_unc] = coin_toss (1000); rel_unc(end) ans = 0.0315 [results, ave_value, rel_unc] = coin_toss (2000); rel_unc(end) ans = 0.0226 [results, ave_value, rel_unc] = coin_toss (2000); rel_unc(end) ans = 0.0223 figure(2) plot(rel_unc,'k*-')