[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Help a retarded new programmer out. <pic>

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 23
Thread images: 3

Help a retarded new programmer out.
<pic>
>>
here's a really dumb way
t[random(0,2)]
>>
>>61177663

Forgot to mention this is a problem from Think Python, i.e python programming language.
>>
>>61177735
hist[key] / sum(hist.values())
>>
>>61177766
btw I don't understand the answer given in >>61177663 at all
>>
>>61177766
My first attempt was using random.random() to generate a number and check if it was <= the probability of hist[key] / sum(hist.values()) with a for loop.
>>
>>61177766
Oh shit sorry I mostly just read the last sentence - that's just going to return the probability of they key you specifically provide.
>>
>>61177608
Naive solution

import random

hist = {'a':2, 'b':1}
a = []
for k,v in hist.items():
for i in range(v):
a.append(k)
print(random.choice(a))
>>
>>61177608
take a random number between 0 and the sum of the histogram entries.

Then just count through each entry, If you rolled a 3 for instance, in the example of above, you'd start at a and count for as many times as a has entries, then you'd go to the next letter, b. You count once for b's one entry and then you stop because your count equals the number you rolled. Then just return whatever number you're on.

Maybe there's a better way that's faster or something but this way is piss easy.
>>
from random import choice
choice(sum([[k] * v for k, v in hist.items()], []))
>>
File: 1497695836097.png (108KB, 400x400px) Image search: [Google]
1497695836097.png
108KB, 400x400px
>>61177946

>kill you are self.
>>
Convert the histogram dict into a list like in your pic.

import random

def choose_from_hist(hist):
t = []
for k, v in hist.iteritems():
t.extend([k] * v)
return random.choice(t)
>>
>>61178008

This was my initial solution as well but I didn't think OP would get the code...

>>61178038
This is great as well
>>
>>61178038
OP this is basically the clear version of what I code golf'd here >>61178008
>>
>>61177893
Glorious, thank you.
>>61177946
Not nice, wtf.
>>
File: 1300044776986.jpg (17KB, 250x250px) Image search: [Google]
1300044776986.jpg
17KB, 250x250px
>import random
>>
>>61178008
That's a great solution I haven't worked much on list comprehensions yet so it was difficult to understand at first.
>>
I don't... understand why this is a question. random.choice literally already does this. 5 minutes of RTFM would have solved this for you: https://docs.python.org/2/library/random.html
>>
ITT: idiots who can't comprehend memory complexity

import random

def choose(hist={}):
choice = None
choices = (
k for k, v in hist.items()
for _ in range(v)
)
choice_idx = random.randint(0, sum(hist.values()))
for _ in range(choice_idx):
last = next(choices)
return last
>>
>>61178687

Holy shit, this is some /r/programminghorror material. I really hope you're trolling.
>>
>>61177608
http://www.keithschwarz.com/darts-dice-coins/
>>
>>61178015
worst 2hu
>>
>>61178687
>Average pythonist
Thread posts: 23
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.