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

  • Home
  • SEARCH
  • 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 744307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:57:47+00:00 2026-05-14T08:57:47+00:00

I am currently writing an app in python that needs to generate large amount

  • 0

I am currently writing an app in python that needs to generate large amount of random numbers, FAST. Currently I have a scheme going that uses numpy to generate all of the numbers in a giant batch (about ~500,000 at a time). While this seems to be faster than python’s implementation. I still need it to go faster. Any ideas? I’m open to writing it in C and embedding it in the program or doing w/e it takes.

Constraints on the random numbers:

  • A Set of 7 numbers that can all have different bounds:
    • eg: [0-X1, 0-X2, 0-X3, 0-X4, 0-X5, 0-X6, 0-X7]
    • Currently I am generating a list of 7 numbers with random values from [0-1) then multiplying by [X1..X7]
  • A Set of 13 numbers that all add up to 1
    • Currently just generating 13 numbers then dividing by their sum

Any ideas? Would pre calculating these numbers and storing them in a file make this faster?

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-05-14T08:57:48+00:00Added an answer on May 14, 2026 at 8:57 am

    You can speed things up a bit from what mtrw posted above just by doing what you initially described (generating a bunch of random numbers and multiplying and dividing accordingly)…

    Also, you probably already know this, but be sure to do the operations in-place (*=, /=, +=, etc) when working with large-ish numpy arrays. It makes a huge difference in memory usage with large arrays, and will give a considerable speed increase, too.

    In [53]: def rand_row_doubles(row_limits, num):
       ....:     ncols = len(row_limits)
       ....:     x = np.random.random((num, ncols))
       ....:     x *= row_limits                  
       ....:     return x                          
       ....:                                       
    In [59]: %timeit rand_row_doubles(np.arange(7) + 1, 1000000)
    10 loops, best of 3: 187 ms per loop
    

    As compared to:

    In [66]: %timeit ManyRandDoubles(np.arange(7) + 1, 1000000)
    1 loops, best of 3: 222 ms per loop
    

    It’s not a huge difference, but if you’re really worried about speed, it’s something.

    Just to show that it’s correct:

    In [68]: x.max(0)
    Out[68]:
    array([ 0.99999991,  1.99999971,  2.99999737,  3.99999569,  4.99999836,
            5.99999114,  6.99999738])
    
    In [69]: x.min(0)
    Out[69]:
    array([  4.02099599e-07,   4.41729377e-07,   4.33480302e-08,
             7.43497138e-06,   1.28446819e-05,   4.27614385e-07,
             1.34106753e-05])
    

    Likewise, for your “rows sum to one” part…

    In [70]: def rand_rows_sum_to_one(nrows, ncols):
       ....:     x = np.random.random((ncols, nrows))
       ....:     y = x.sum(axis=0)
       ....:     x /= y
       ....:     return x.T
       ....:
    
    In [71]: %timeit rand_rows_sum_to_one(1000000, 13)
    1 loops, best of 3: 455 ms per loop
    
    In [72]: x = rand_rows_sum_to_one(1000000, 13)
    
    In [73]: x.sum(axis=1)
    Out[73]: array([ 1.,  1.,  1., ...,  1.,  1.,  1.])
    

    Honestly, even if you re-implement things in C, I’m not sure you’ll be able to beat numpy by much on this one… I could be very wrong, though!

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

Sidebar

Ask A Question

Stats

  • Questions 416k
  • Answers 416k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I recommend that you read up further on Symfony to… May 15, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer You can get the current URL from the UIWebView's request… May 15, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer I ended up scavenging through a friends code and found… May 15, 2026 at 9:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.