www.gusucode.com > stats 源码程序 matlab案例代码 > stats/GenerateRandomNumbersFromGeometricDistributionExample.m
%% Generate Random Numbers from Geometric Distribution % % Copyright 2015 The MathWorks, Inc. %% % Generate a single random number from a geometric distribution with % probability parameter _p_ equal to 0.01. rng default % For reproducibility p = 0.01; r1 = geornd(0.01) %% % The returned random number represents a single experiment in which 20 % failures were observed before a success, where each independent trial has % a probability of success _p_ equal to 0.01. %% % Generate a 1-by-5 array of random numbers from a geometric % distribution with probability parameter _p_ equal to 0.01. r2 = geornd(p,1,5) %% % Each random number in the returned array represents the result of an % experiment to determine the number of failures observed before a success, % where each independent trial has a probability of success _p_ equal to % 0.01. %% % Generate a 1-by-3 array containing one random number from each of the % three geometric distributions corresponding to the parameters % in the 1-by-3 array of probabilities _p_. p = [0.01 0.1 0.5]; r3 = geornd(p,[1 3]) %% % Each element of the returned 1-by-3 array |r3| contains one random number % generated from the geometric distribution described by the corresponding % parameter in |P|. For example, the first element in |r3| represents an experiment % in which 127 failures were observed before a success, where each % independent trial has a probability of success _p_ equal to 0.01. The % second element in |r3| represents an experiment in which 5 failures % were observed before a success, where each independent trial has a % probability of success _p_ equal to 0.1. The third element in |r3| represents % an experiment in which zero failures were observed before a success - in % other words, the first attempt was a success - where each independent % trial has a probability of success _p_ equal to 0.5.