Discover MakerZone

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

Thread Subject:
probability

Subject: probability

From: Stuart

Date: 6 Dec, 2010 04:14:05

Message: 1 of 7

hi. i want to familiarise myself with probability in matlab, and eventually write like a matlab casino for fun. currently im going to code a roulette wheel. what functoins would be best suited for probability. ages ago i wrote a version of this where i took the random number generation function (cant remember what its called) and had to multiply it by 32 or somethign, and then use another function to see whether the number was even. not very efficient for betting odd or even.

but say i want an output of yes or no if i give something a like 50% probability and run that in a loop, what functions would be useful? Matlab help is hard to navigate if u dont know the name of what your searching for

Subject: probability

From: ImageAnalyst

Date: 6 Dec, 2010 04:19:15

Message: 2 of 7

You mean like this:?
for k = 1 : numberOfSpins
  yesno = rand>0.5
  if yesno
    % do stuff
  else
    % Do other stuff
  end
end

Subject: probability

From: Stuart

Date: 6 Dec, 2010 06:25:04

Message: 3 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5953beb8-0d4a-445e-bd4f-1b62e06eca0f@g25g2000yqn.googlegroups.com>...
> You mean like this:?
> for k = 1 : numberOfSpins
> yesno = rand>0.5
> if yesno
> % do stuff
> else
> % Do other stuff
> end
> end

I was hoping that there was another method of designating probability but that seems simple enough thanks, i think i used that a while ago

Subject: probability

From: Walter Roberson

Date: 6 Dec, 2010 08:14:39

Message: 4 of 7

On 06/12/10 12:25 AM, Stuart wrote:
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <5953beb8-0d4a-445e-bd4f-1b62e06eca0f@g25g2000yqn.googlegroups.com>...
>> You mean like this:?
>> for k = 1 : numberOfSpins
>> yesno = rand>0.5
>> if yesno
>> % do stuff
>> else
>> % Do other stuff
>> end
>> end
>
> I was hoping that there was another method of designating probability
> but that seems simple enough thanks, i think i used that a while ago

Out of curiosity: what kind of method of designating probability were
you envisioning?


You could use

YN = {'yes', 'no'};
yesno = YN{1+rand>p};

if you wanted it in terms of the string 'yes' or 'no'.


With newer versions of Matlab, you can use randi() to generate a random
integer. With older versions, you should ceil(rand*N) where N is the
maximum integer and 1 is the minimum.


The probability of "even" is not 1/2: it is either 18/37 or 18/38
depending whether the table only has 0 or has both 0 and 00.

Subject: probability

From: Peter Perkins

Date: 6 Dec, 2010 15:18:35

Message: 5 of 7

On 12/6/2010 1:25 AM, Stuart wrote:
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <5953beb8-0d4a-445e-bd4f-1b62e06eca0f@g25g2000yqn.googlegroups.com>...
>> You mean like this:?
>> for k = 1 : numberOfSpins
>> yesno = rand>0.5
>> if yesno
>> % do stuff
>> else
>> % Do other stuff
>> end
>> end
>
> I was hoping that there was another method of designating probability
> but that seems simple enough thanks, i think i used that a while ago

One alternative might be

for k = 1 : numberOfSpins
   if randi(1) % generate a one or a zero
     % do stuff
   else
     % Do other stuff
   end
end

but that's going to do exactly what ImageAnalyst's code does.

Subject: probability

From: Stuart

Date: 6 Dec, 2010 15:59:07

Message: 6 of 7



currently, using a 33 numbered table , 0:32 (i didnt google how many there actually are on a table) im using this code to see random betting on numbers. im writing one with a input interface now but the betting on odd or even is causing a problem. as this is for fun im not real fussed about fixing it although im sure it will be a minor error

This is the 100 bet simulation with random numbers betting on
clear all
clc



Cash = 1000;
for i = 1:100
bet = 10;
b(i) = rand*32;
bet_number(i) =ceil(b(i));

a(i) = rand*33;

Result(i) = ceil(a(i)) -1;
if Result(i) == 33
    Result(i) = 32;
end
if bet_number(i) == Result(i)
    Winnings(i) = bet*32;
    
    Cash(i+1) = Cash(i) + Winnings(i);
    
else
    Cash(i+1) = Cash(i) - bet;
end
end
vector= 1:1:i;

final_amount = Cash(i+1)
figure(1)
plot(vector,bet_number, 'b', vector,Result,'r')
grid on
figure(2)
plot (vector,Cash(2:end))

Subject: probability

From: Steven_Lord

Date: 6 Dec, 2010 22:25:48

Message: 7 of 7



"Stuart " <imanotarat@gmail.com> wrote in message
news:idj18b$e0l$1@fred.mathworks.com...
>
>
> currently, using a 33 numbered table , 0:32 (i didnt google how many there
> actually are on a table)

Roulette? A standard American roulette wheel has 38 numbers: 1 through 36,
0, and 00. A standard French roulette wheel, according to Wikipedia, has 37
spaces: 1 through 36 and 0.

> im using this code to see random betting on numbers. im writing one with a
> input interface now but the betting on odd or even is causing a problem.
> as this is for fun im not real fussed about fixing it although im sure it
> will be a minor error
> This is the 100 bet simulation with random numbers betting on
> clear all
> clc
>
>
>
> Cash = 1000;
> for i = 1:100
> bet = 10;
> b(i) = rand*32;

Rather than performing floating-point arithmetic here, why not just use
RANDI to generate random integers from 0 to 37 (where 37 represents the 00
space)?

*snip*

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us