2
$\begingroup$

Thought this would be a nice puzzle for some. :)

First, I take a random number from 1 to 10, then take that number and multiply it by 5, we'll call this result 'threshold'. Then I take another new random number from 1 to 100, and see if it is equal or lower than the 'threshold' number.

pseudocode:

th = rand(10) * 5; res = rand(100); if (res <= th) return TRUE; 

Is this the simplest way to calculate this? I'm thinking one of rand(500) or rand(1000) would be the same, but I can't get my probability formulas to work.

  • 0
    Why would you do something like this? On that note: the straightforward approach does what you want, so why do you expect something simpler?2011-08-25
  • 6
    Equivalent would be "if (rand(1000) <= 275) return TRUE;" as the probability of your program returning TRUE is 27.5%.2011-08-25

2 Answers 2