Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:57:51+00:00 2026-06-04T00:57:51+00:00

from random import uniform prob = [0.25,0.30,0.45] def onetrial(prob): u=uniform(0,1) if 0 < u

  • 0
from random import uniform

prob = [0.25,0.30,0.45]

def onetrial(prob):
    u=uniform(0,1)
    if 0 < u <= prob[0]:
        return 11
    if prob[0] < u <= prob[0]+prob[1]:
        return 23
    if prob[0]+prob[1] < u <= prob[0]+prob[1]+prob[2]:
        return 39

print onetrial(prob)

I wonder how to reduce the repetitive part in the def using some for-loop techniques. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:57:54+00:00Added an answer on June 4, 2026 at 12:57 am

    Assuming you call onetrial frequently, calculate the CDF first to make it a bit faster:

    from random import uniform
    
    vals = [11, 23, 39]
    prob = [0.25, 0.30, 0.45]
    cdf = [sum(prob[0:i+1]) for i in xrange(3)]
    
    def onetrial(vals, cdf):
        u = uniform(0, 1)
        for i in range(3):
            if u <= cdf[i]:
                return vals[i]
    

    You could use bisect to make it even faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i did this code : from scitools.std import * npoints=10 vectorpoint=array(random.uniform(-1,1,[1,2])) experiment=array(random.uniform(-1,1,[npoints,2])) print(vectorpoint=,vectorpoint) print(experiment=,experiment)
from sys import exit from random import randint class Map(object): def death(): print quips[randint
I am creating a random ID using the below code: from random import *
I am using this code to generate random text : from collections import defaultdict,
I have some question about functions which have default parameters. import sys from random
from random import * while True: random1 = randint(1,20) random2 = randint(1,20) print(h =
I'm using Java to parse HTML from a random website, let's say it's http://google.com
I'm trying to return the message field from a random DB entry (in the
I have been studying this code to generate random text: from collections import defaultdict,
I'm used to typing random.randrange . I'll do a from random import Random to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.