Code covered by the BSD License  

Highlights from
Random number with specified probability

5.0 | 1 rating Rate this file 32 Downloads (last 30 days) File Size: 1.24 KB File ID: #25481 Version: 1.2

Random number with specified probability


Mo Chen (view profile)


03 Oct 2009 (Updated )

Random number from a discrete distribution

| Watch this File

File Information

Generate samples from a discrete distribution (i.e. multinomial) specified by discrete probability parameters p.

x = discreternd(p,n);

p is a vector of the discrete probabilites (sum(p)==1)
n is the number of samples you want.

if length(p)==d, then x is a vector of n integers in [1,d].


>> discreternd([.2,.3,.5],10)

ans =

     1 3 3 2 3 1 2 3 3 3

MATLAB release MATLAB 7.9 (R2009b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
03 Nov 2014 Adel

Adel (view profile)

I need to generates RN between 0 and 0.9

Comment only
06 Apr 2011 Tim Benham

Tim Benham (view profile)

It means someone has already posted a function that uses the same method.

Comment only
09 Feb 2010 Mo Chen

Mo Chen (view profile)

Forgive my poor English, but what does it suppose to mean?

Comment only
09 Feb 2010 Jos (10584)

Jos (10584) (view profile)

A case of cryptomnesia?

Comment only
26 Jan 2010 Nima Moshtagh

Nima Moshtagh (view profile)

07 Nov 2009 1.1

greatly speed up by utilizing build in histc function

29 Feb 2012 1.2

update description

Contact us