• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Birthday Simulation

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i'm trying to answer the questions "Minimum number of people needed to have more than a 50% chance for a pair to have the same birthday" and "Empirical probability of there being a pair of people with the same birthday in a group of 100 people." My program works fine when answering the first question, but not for the second one. I keep getting 1.01, but I know that's not correct. Does anyone know why simTwo is always 1.01? When I run the debugger in vs code and change the value of T myself, it works fine. Since it's 1.01 I think it's saying that there is a match every single time, but I know that's not true.

 
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still looking ... but your match calc is tortured.

 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan González wrote:I keep getting 1.01, but I know that's not correct.


Why is it not correct? What do you think it should be?
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't 1.01 mean that there is over a 100% chance of there being a match? I may just be interpreting the problem wrong.
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use the same simulation method to answer both questions. This is the main() I came up with and the second question is performed without a while() loop.
Output:
n: 23  p: 0.59
n: 56  p: 1.0
n: 100  p: 1.0
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I don't really understand what you mean.
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it is floating point rounding error with t++. I used an int for 't'.
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll keep trying to figure this out, I appreciate you taking a look
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try changing 't' to be an int and then only at the final stage divide it by 100.0.
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After changing T to an int

If I divide by 100 at the end I get 1.0, but if I divide by 100.0 I still get 1.01.
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change simulation() to take an int for 't' and then define the method as returning an int. Then divide like this
simTwo = simulation(100, 0)/100.0;
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure you're dealing with floating point rounding error.
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HERE IT IS
for(i = 0; i <= 100; i++){
this loops for 101 iterations.
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that works, it now says 1.0! Does the probability of there being a pair of people with the same birthday in a group of 100 people being 100% (1.0) make sense?
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Juan González
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you so much for your help!
 
Carey Brown
Bartender
Posts: 10966
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help.

FYI

loop are generally written like this
          for(i = 0; i < 100; i++){
using the '<' operator
 
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan González wrote:that works, it now says 1.0! Does the probability of there being a pair of people with the same birthday in a group of 100 people being 100% (1.0) make sense?


You simulate much, by the look of it. Have you tried to come up with a formula for the chance of no same anniverseries in a group of N people?
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Juan González wrote:that works, it now says 1.0! Does the probability of there being a pair of people with the same birthday in a group of 100 people being 100% (1.0) make sense?



It's pretty darn close to 100%, yes.

Have a look at the Wikipedia article Birthday problem; go to the section headed "Approximations" and at the far right you'll see two graphs which answer that question.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or look at the table where it says:

100 99.99997%
 
The only cure for that is hours of television radiation. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic