MATLAB Answers

## How to generate a Bernoulli distributed binary data

Asked by Mallikarjuna

### Mallikarjuna (view profile)

on 1 Jul 2013

Hi, I am trying to generate a Bernoulli distributed binary data. please help.

## Products

No products are associated with this question.

## 2 Answers

### Wayne King (view profile)

Answer by Wayne King

### Wayne King (view profile)

on 1 Jul 2013
Edited by Wayne King

### Wayne King (view profile)

on 1 Jul 2013

Do you have the Statistics Toolbox?

You have to specify the parameter for the distribution, which is the probability of a "success". Here I'll use p = 0.25

```     pd = makedist('Binomial','N',1,'p',0.25);
x = random(pd,100,1);```

If you want the simple case where p = 0.5

```x = randi([0 1],100,1);
```

Mallikarjuna

### Mallikarjuna (view profile)

on 2 Jul 2013

I am getting an error saying, 'makedist function is not supported'! Is there any other way of doing it without using the statistics toolbox?

### Shashank Prasanna (view profile)

Answer by Shashank Prasanna

### Shashank Prasanna (view profile)

on 2 Jul 2013

You probably have an older version of MATLAB installed.

Try the following instead:

http://www.mathworks.com/help/stats/binornd.html

Check the output of 'ver' command to confirm you have the statistics toolbox.

If not there is always FEX:

http://www.mathworks.com/matlabcentral/fileexchange/7309-randraw

#### Join the 15-year community celebration.

Play games and win prizes!

Learn more

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

### MATLAB Academy

New to MATLAB?

Learn MATLAB today!