Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help re: gap in interpretation of two while loops.  RSS feed

 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks.

I've been stuck on a problem for a few hours now. These two while statements which I think
should translate very similarly produce ints with a huge gap between their values.

What I'm trying to do is test how many random people i must meet whose birthdays'
can fill everyday of the year (not accounting for leap years):



I'm assuming that the first while loop disregards every person previously met after each birthday test,
as the second while loop using the array still accounts for previous persons met (although I don't really
see how that's possible within the code)?

The first while loop usually comes out to a number around randTests = 120000
whereas the latter usually produces a number more reasonable (in my opinion) such as arrTests = 2500.

I understand that they are both "random" tests and can ostensibly produce any possible number.
But after running the program a few times, something just doesn't look right with what the first while
loop produces.

Any insight would be much appreciated. Thanks!
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither of these loops looks to me to do what you say you are trying to do.

The first one takes the numbers 0 through 364 in turn, and for each one asks "How many random numbers between 0 and 364 do I have to test to hit this number?" For each number, it will take you 365 tests to have a 50-percent chance of matching your number, and then you move on to the next one. So you have 365 * 365 tests to do, 133k, roughly the number you say you get. I'm not sure what that has to do with your birthday, but the number you're getting looks reasonable to me.

The next one gets a random number 0-364 and marks when it hits a number that has not been hit before. This is beyond me in terms of calculating a likely outcome, but having it be a couple orders of magnitude less than the first one looks, again, reasonable to me. You don't have to spend all those tests looking for ONE specific number; you're looking for any number you have not hit before, until you have hit them all. When you have 1 number left, you have a 1 in 365 chance of hitting it any one time; when you have 2 left, it is a 2 in 365 chance, etc. -- someone who knows (and remembers) more about series than I do can likely calculate a sigma for this 8>).

rc
 
Juan Marcos
Greenhorn
Posts: 12
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for putting it into very understandable term, Ralph!

At least now I know specifically what each loop is doing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!