Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I really feel ashamed to ask this question however I don't have time for revision. Also not a native English speaker, so excuse my lack of math vocabulary.

I am writing a program that requires assigning probabilities to variables then selecting one randomly.

Example:

Imagine that I have I coin, I would like to assign the probably of 70% to heads and 30% to tails. When I toss it I would like to have 70% chance that the heads appears and 30% tails.

A dumb way to do it is to create an array of cells insert the heads 70 cells and the tail in 30. Randomize them and select one randomly.

I would like to be pointed to how I can do it mathematically, Without creating the array.

Edit 1: I also would like to point out that I am not limited to 2 variables. For example lets say that I have 3 characters to select between (*,\$,#) and I want to assign the following probably to each of them * = 30%, \$ = 30%, and # = 40%.

That's why I did not want to to use the random function and wanted to see how it was done mathematically.

share|improve this question
1  
What is the question? –  Stefan Hansen Jan 10 '13 at 7:50
    
I would like to be pointed to how I can do it mathematically, Without creating the array. –  thedethfox Jan 10 '13 at 7:54
    
whatever language you are using to write your program, there is always a random() method/function/procedure. Generate a random number between o and 1, and if it is greater than 0.7 say it's tails, otherwise it's heads. –  Djaian Jan 10 '13 at 7:59

4 Answers 4

up vote 1 down vote accepted

Let $X\sim U(0,1)$ be uniformly distributed over $(0,1)$. Then $X$ has distribution function $$ F_X(x)=P(X\leq x)=x,\quad x\in (0,1). $$ Define a new variable $Y$ by $$ Y(\omega)= \begin{cases} 1,\quad &\text{if }X(\omega)\leq 0.7,\\ 0,&\text{if }X(\omega)>0.7, \end{cases} $$ or in other words $Y=1_{\{X\leq 0.7\}}$. Then $$ P(Y=1)=P(X\leq 0.7)=0.7,\qquad\text{and}\qquad P(Y=0)=0.3, $$ Now let $1$ mean heads and $0$ mean tails. In R this easily done by x=runif(1) y=(x<=0.7).


This extends naturally to several variables. If you want probabilities $30\%$, $30\%$ and $40\%$ then let $$ Y(\omega)= \begin{cases} 0, \quad &\text{if }X(\omega)\leq 0.3,\\ 1,&\text{if }0.3<X(\omega)\leq 0.6,\\ 2,&\text{if }X(\omega)>0.6. \end{cases} $$ Do you see the pattern?

share|improve this answer
    
thumbs up (+1). But again I have to revise my math as I have not used probability since years and now it looks like voodoo to me. –  thedethfox Jan 10 '13 at 8:07

Throw your (fair) coin four times. Amongst the 16 possible results, decide that 3 results mean character *, 3 others mean character $, and 4 others mean character #. This leaves you with 6 unallocated results. If one of these is the result of the throw of your four coins, decide that this round was not successful and throw the coin again four times to apply the same allocation procedure. If this new round fails, starts again, and so on until you get one of the 10 allocated results. The mean number of rounds before you are able to produce a character is only 1.6 hence the mean number of coin tosses is 6.4.

share|improve this answer
    
+ 1 for the answer. However Stephan has posted a good one. –  thedethfox Jan 10 '13 at 8:21
    
Quote: I did not want to use the random function... ?? –  Did Jan 10 '13 at 8:25
    
What I meant is to use the built in random function. Sorry for this mate. –  thedethfox Jan 10 '13 at 8:27
1  
Same spirit (These are very common "interview" questions): Suppose you have a rand7 function that generates integers 1 through 7 each with prob. 1/7. How to get the function rand4 (from rand7) that generates integers 1 through 4 each with prob. 1/4? One thing I don't like about these solutions is that you may need to do infinite coin tosses before you can actually generate one sample. –  Anon Jan 10 '13 at 8:28
    
@Anon Exactly. I was thinking the same thing. –  thedethfox Jan 10 '13 at 8:30

Stefan Hansen's answer is obviously correct. I myself took this as a "programming question," and here is a one-liner :)

Let $X$ be uniformly distributed on $[0,1]$. Let "Heads" correspond to $1$, and "Tails" correspond to $0$. Then, the random variable $Y = \min\{\lfloor \frac{10X}{3} \rfloor, 1\}$ has the desired properties.

share|improve this answer
    
+1. For the answer. –  thedethfox Jan 10 '13 at 8:13

Mathematically: Let X be a random variable on $\{T,H \}$ such that $X(T)=0.3$ and $X(H)=0.7$.

Using R (a commonly used statistical software), and following your suggested method: coin <- c("H","H","H","H","H","H","H","T","T","T")

sample(coin, 100, replace = TRUE)

the above will give you 100 results of tossing a coin as you described.

A more straightforward way:

a <- runif(100, 0, 1)

b <- a < 0.3

the above will give you a vector with the results of 100 coin tosses of your coin, where the value TRUE in the vector stands for a result of Tail.

share|improve this answer
    
+ 1 for the answer. However Stephan has posted a good one. –  thedethfox Jan 10 '13 at 8:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.