Create a List of 0's or 1's chosen based on probability?

This is the place for queries that don't fit in any of the other categories.

Create a List of 0's or 1's chosen based on probability?

Postby skc » Sun Apr 28, 2013 2:36 pm

Hey guys I'm very new to Python and I'm trying to create a list that automatically populates with zeros or ones based on a preset probability? Any help would be hugely appreciated!
Last edited by Yoriz on Sun Apr 28, 2013 3:16 pm, edited 1 time in total.
Reason: Changed to a more descriptive title
skc
 
Posts: 1
Joined: Sun Apr 28, 2013 2:28 pm

Re: Creating Lists in Python

Postby setrofim » Sun Apr 28, 2013 3:02 pm

Code: Select all
>>> import random
>>> [0 if random.random() < 0.4 else 1 for i in xrange(10)]
[1, 1, 1, 0, 1, 1, 1, 1, 0, 0]
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests