• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

program to calculate male to female ratio of a given number

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i have the following code, and it works correctly, but im not sure how to make the results be partially biased towards the male side to represent the slightly greater number of males in the world. it is male: 50.34%, female: 49.66%. how can i incorporate this into the program? examples are welcome. thank you in advance.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before even thinking about writing a line of code, you should work out the math behind the problem. Give that a go.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nathan gibson wrote:boolean isMale = randomNumber > .5;


Just as a side note, you do know that the changes of being a female are larger than those of being a male this way, right?

randomNumber is between 0 (inclusive) and 1 (exclusive). For simplicity's sake let's only deal with steps of 0.01.
Someone is female for all values from 0 to 0.5 (inclusive) - that's 51 different values. Someone is male for all values from 0.51 to 0.99 (inclusive) - that's 49 different values. To have an even 50-50 match, turn the > into a >=.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nathan gibson wrote:it is male: 50.34%, female: 49.66%. how can i incorporate this into the program?


That would be very easy. You'd just have to change one thing in this line:

What do you think you'd have to change there to make the chance of isMale being true to .5034?
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely that's < 0.5, Jesper???
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Let me put this forward this way:

for simplicity, lets assume random gives you a value between 1 to 10 (both inclusive)
assuming that you want ratio of men >= 70% and female <=30%
wouldnt you say that if random <=7 its a Man else a Woman ?


now change the example to match 50.34% with random 0 to 1
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh. okay. i got ya, sorry, that was due to a bit of programmers block, thanks for the help. you guys are great.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nathan,

I've noticed that this is the same program what you posted here. Only the variable names and interpretations are different. Can I ask you what's the purpose of this? I'm really curious.
According to the answers you got here you should also change the randomNumber > .5; to randomNumber >= .5; in the coin flipping program.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Surely that's < 0.5, Jesper???


I just copied and pasted a line of code out of Nathan's original program.
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:I just copied and pasted a line . . .

Which will have well over 0.00000000000001% inaccuracy
 
I like tacos! And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic