1

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.

  • 1
    What is the question? – Stefan Hansen Jan 10 '13 at 07:50
  • I would like to be pointed to how I can do it mathematically, Without creating the array. – thedethfox Jan 10 '13 at 07: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 07:59

4 Answers4

3

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.

Did
  • 279,727
  • Quote: I did not want to use the random function... ?? – Did Jan 10 '13 at 08:25
  • What I meant is to use the built in random function. Sorry for this mate. – thedethfox Jan 10 '13 at 08: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 08:28
  • @Anon Exactly. I was thinking the same thing. – thedethfox Jan 10 '13 at 08:30
  • So, I did not want to use the random function means that you want to use the built in random function. O well... :-) – Did Jan 10 '13 at 08:30
  • Again dude sorry for that :-( don't make me feel guilty about it. I have already given you +1. I cannot give you a +2, can I? – thedethfox Jan 10 '13 at 08:31
  • @Anon you may need to do infinite coin tosses... This is wrong, there is even a theorem stating the opposite... :-) (Do you know that, if your misgivings about this procedure are real, you might as well decide that the best way to inflate a flat tyre is to open the valve and to wait for the oscillations in the ambient pressure to inflate the tyre all by themselves?) – Did Jan 10 '13 at 08:33
  • thedethfox: Just trying to understand what was your question, after all. And I am afraid I still don't get it. – Did Jan 10 '13 at 08:34
  • @did, fair enough, I like to abuse infinity :) You may need to do an arbitrarily large number (put your favorite natural number here) of coin tosses before you can actually generate one sample. – Anon Jan 10 '13 at 08:36
  • @Anon True and off the point: for all practical purposes, the number of coin tosses will be finite, and even rather small. – Did Jan 10 '13 at 08:40
  • @did BTW, I think infinity works too. I guess you were thinking about Kolmogorov's zero-one law. The fact that it is a zero-probability event doesn't mean you will avoid it, isn't that correct? – Anon Jan 10 '13 at 08:44
  • @Anon No. You might as well say that to draw at random a number between 0 and 1 will yield the result 42. Almost sure statements are the strongest possible ones in probability hence one may never avoid an event more strongly than by making sure its probability is zero. – Did Jan 10 '13 at 08:52
  • @did No. Draw at random a real number between 0 and 1. Wait, what you get should not have happened almost surely. I won't get 42 as it is not in my sample space. – Anon Jan 10 '13 at 09:02
  • @Anon And what if your sample space is $(\mathbb R,\mathcal B(\mathbb R))$ endowed with the restriction to $[0,1]$ of the Lebesgue measure on $\mathbb R$? This modelization of the experiment is as legitimate as the ones you may have in mind since all yield exactly the same set of results. (By the way, what you mean by what you get should not have happened almost surely is opaque to me.) – Did Jan 10 '13 at 09:08
  • @did How about the discussion here (we are going off-topic anyway): http://math.stackexchange.com/questions/41107/zero-probability-and-impossibility – Anon Jan 10 '13 at 09:12
  • @did BTW, reading my first comment, you may have taken it offensively, that was not my intention. If that is the case, my apologies. I find these (your answer) methods elegant, but they have the weakness I mentioned. My method (answer) is much worse :) – Anon Jan 10 '13 at 09:15