# a matlab code of getting a value uniformly at random from a the set {-1,1}

4 views (last 30 days)

Show older comments

I need to get a value uniformly at random from a the set {-1,1}

##### 0 Comments

### Answers (5)

Massimo Zanetti
on 11 Oct 2016

Edited: Massimo Zanetti
on 12 Oct 2016

If you look for a real number in the interval [-1,1], here is it

n = 2*rand-1

If you look for an integer either 1 or -1, here is it

n = 2*randi(2)-3

##### 2 Comments

Massimo Zanetti
on 12 Oct 2016

The first one returns a random REAL number in the interval [-1,1]. That is why it doesn't return only -1 or 1.

The second one, I have fixed it. Now it returns a random integer among {-1,1}.

Matthew Eicholtz
on 11 Oct 2016

To generate values uniformly from the set {-1,1}, use:

N = 100;

x = 2*(rand(N,1)>0.5)-1;

where N is the number of values you want.

##### 0 Comments

Walter Roberson
on 12 Oct 2016

>> N=10000000;

>> timeit(@() 2*(rand(N,1)>0.5)-1, 0)

ans =

0.1797

>> timeit(@() 2*randi(2,N,1)-3, 0)

ans =

0.1467

>> timeit(@() 2*round(rand(N,1))-1, 0)

ans =

0.1269

##### 1 Comment

Walter Roberson
on 14 Oct 2016

Using

N=10000000;

clear x;tic;x=2*round(rand(N,1))-1;toc %"round"

clear x;tic;x=2*(rand(N,1)>0.5)-1;toc %"rand>"

clear x;tic;x=2*randi(2,N,1)-3;toc %"randi"

and taking the lowest of several timings for each of the three, I record

same Virtual machine with Windows 10 (host is Mac)

- R2011b: round 0.169, rand> 0.187, randi 0.218
- R2012b: round 0.165, rand> 0.191, randi 0.220
- R2013b: round 0.172, rand> 0.193, randi 0.220
- R2015a: round 0.179, rand> 0.180, randi 0.169
- R2015b: round 0.176, rand> 0.184, randi 0.155
- R2016a: round 0.166, rand> 0.178, randi 0.156
- R2016b: round 0.165, rand> 0.176, randi 0.150

So round did not change much; rand> got slightly faster; and randi improved a fair bit

Native on host (OS-X El Capitan)

- R2014b: round 0.141, rand> 0.214, randi 0.144 %but randi was faster on average
- R2015a: round 0.140, rand> 0.238, randi 0.148 %but randi was faster on average
- R2015b: round 0.127, rand> 0.169, randi 0.143
- R2016a: round 0.105, rand> 0.144, randi 0.122
- R2016b: round 0.093, rand> 0.165, randi 0.117

so the three all got faster, but not all at the same time

The times to process rand> were sometimes worse on native OS-X than on emulated Windows 10, but the final times are pretty comparable; it might even be the case that if I were to boot into Windows 10 that the times for rand> might be better than running it in OS-X .

The final processing times for round are much better on OS-X

randi appears to have gotten faster on OS-X as of R2016a.

I note that the timing of randi getting faster differs between operating systems.

James Tursa
on 11 Oct 2016

Edited: James Tursa
on 11 Oct 2016

Yet another way (although Matthew's Answer is faster so I voted for it):

N = number of values

R = 2*randi(2,N,1) - 3;

##### 6 Comments

Walter Roberson
on 12 Oct 2016

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!