# Generate random sequence of 1's and -1's in matlab

1. ## Generate random sequence of 1's and -1's in matlab

How do i generate a random sequence of 1's and minus -1's in matlab with mean 0?

•

2. ## Re: Generate random sequence of 1's and -1's in matlab

Code:
```x0 = 1:40;
y0 = 2*int32( randn( size(x0) ) >= 0.0 ) - 1;```

•

3. ## Re: Generate random sequence of 1's and -1's in matlab

This doesnt always give a mean of zero

•

4. ## Re: generate random 1's and -1's in matlab

the rand function generates uniformly distributed random numbers

if you tell it to generate random numbers between 0 and 1,
you can convert those less than 0.5 to -1 and those greater than 0.5 to 1

5. ## Re: Generate random sequence of 1's and -1's in matlab

This doesnt always give a mean of zero
By nature of random distribution, an arbitrary selected sample can't have exactly zero mean otherwise it's not random.

You may want to specify additional properties of the random distribution, e.g. maximal imbalance for a given sample length.

•

6. ## Re: Generate random sequence of 1's and -1's in matlab

Originally Posted by Chinmaye
This doesnt always give a mean of zero
If length of random sequence is enough large, average is zero.

--[[ ]]--