Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
February 12th, 2013, 10:51 AM
 synapsus
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation Power: 0
Need help carrying out a probability program

Hi guys, I've done a good amount of research on this, but I couldn't seem to find the answer. If I could just get a tutorial on doing this, then I can do it myself - I just need to know how!
As for background, I learned basic Python (though the vast majority of it was using turtle, even though I wanted to learn other tools) a year ago or so, but I mostly forgot it. So just in case, pretend I'm a beginner to Python. I'm just gonna copy and paste my problem from the question I posted on Yahoo Answers:

I had an idea for a project in Python, but I'm having trouble finding out how to do it.

Basically, the project is designed to give you a random life - It would start with country - I would give the program a probability of living in a country. Then, based on that country's statistics, I will find out the probability of being a male or a female. After that, I would do class, money, family, etc. Each aspect would be dependent on the last result.

Anyway, so I'm wondering how to carry out the probability part. So, the program would run it, and find a random country as an output based on the probability statistics, then gender based on that, etc.

Thanks for all answers, if you have any questions about my question, I'll be more than happy to answer them!

#2
February 12th, 2013, 12:38 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,147
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 44 m 41 sec
Reputation Power: 455
Code:
```import random
import bisect

POPULATION_BY_MAJOR_POLITICAL_BOUNDARY = dict(USA = (
#(member_of_group , population),
('California',  38041430,),
('Texas'     ,  26059203,),
('New York'  ,  19570261,),
('Florida'   ,  19317568,),
('Illinois'  ,  12875255,),
))

def choose(ITERABLE_OF_2TUPLES):
CUMSUM = [0]
for T in ITERABLE_OF_2TUPLES:
CUMSUM.append(CUMSUM[-1]+T[1])
A = random.randrange(CUMSUM[-1])
B = bisect.bisect_left(CUMSUM[1:],A)
return ITERABLE_OF_2TUPLES[B][0]

# pretend you just ran choose to select a country.  choose returned 'USA'
# COUNTRY = choose(POPULATION_BY_COUNTRY)
COUNTRY = 'USA'

# use the result of choose as input to another selection
for i in range(88):
LOCAL_POLITICAL_REGION = choose(POPULATION_BY_MAJOR_POLITICAL_BOUNDARY[COUNTRY])
print(LOCAL_POLITICAL_REGION)
#etcetera```
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : February 13th, 2013 at 01:03 PM. Reason: Program logic error corrected

#3
February 13th, 2013, 12:24 AM
 synapsus
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg Code: ```import random import bisect DATA = ( #(member_of_group , population), ('California', 38041430,), ('Texas' , 26059203,), ('New York' , 19570261,), ('Florida' , 19317568,), ('Illinois' , 12875255,), ) def choose(ITERABLE_OF_2TUPLES): CUMSUM = [0] for T in ITERABLE_OF_2TUPLES: CUMSUM.append(CUMSUM[-1]+T[1]) A = random.randrange(CUMSUM[-1]) B = bisect.bisect_left(CUMSUM,A) return ITERABLE_OF_2TUPLES[B][0] choose(DATA)```

thanks! i really appreciate it. i'll try and dissect that code and use it.

just a quick question - what if i have more than one probability parameter? can i combine the statistics without having to make a lot of groups? what i mean is that i would list countries and population, then each country would have certain statistics for gender (for example, 51% men, 49% women), class (for example, 2% are high class, etc.), and religion (75% christian, etc.). i want to include these in the random choosing and assign probability to them because of the percentages.

thanks for the response!

#4
February 13th, 2013, 10:05 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,147
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 44 m 41 sec
Reputation Power: 455
I'm just showing that if the groups are in ratios
3 A's to 2 B's to 5 C's then the probability of a single random sample from the group being A is

3 / (3+2+5)

And I wrote code to express that.

I would use the code by first picking a country,
next choosing sex,
then income.

In other words, I'd keep this simple by calling the function as is for successive distributions.

#5
February 13th, 2013, 10:54 AM
 synapsus
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 24 m
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg I'm just showing that if the groups are in ratios 3 A's to 2 B's to 5 C's then the probability of a single random sample from the group being A is 3 / (3+2+5) And I wrote code to express that. I would use the code by first picking a country, next choosing sex, then income. In other words, I'd keep this simple by calling the function as is for successive distributions.

ok, i see - thanks. just need some clarification on the code.

by any chance, can i use what the tool "choose" got as an output as a variable? for example, if i run the program, and get a state, then i want to use whatever state i get into the next program (which will be sex, or something else).

also, how do i write the output? i tried using print(choose(DATA)) but it keeps choosing random states even if i set the population to 0.

related to the last question, i think i'm having some trouble deciphering and understanding the code and why everything is there - that might be why i'm getting weird outputs even if i change population. a short explanation would be awesome, but otherwise i can always follow the code to some extent as long as it works!

thank you so much, and sorry for the beginner questions, i'm really rusty on python lol

#6
February 13th, 2013, 01:04 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,147
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 44 m 41 sec
Reputation Power: 455
The program I posted is modified to perhaps answer your question, also there was a logic error which is fixed.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Need help carrying out a probability program

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |