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 6997353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:16:22+00:00 2026-05-27T20:16:22+00:00

Context Say, we want to use the Box-Muller algorithm. Starting from a couple of

  • 0

Context

Say, we want to use the Box-Muller algorithm. Starting from a couple of random numbers U1 and U2, we can generate G1 and G2. Now, for any call, we just want to output either G1 or G2. In other languages, we could use static variables to be able to know if we need to generate a new couple. How can it be done in Python?

First thoughts

I know, much had been said about static variables in Python, I also must say I’m a bit confused as the answers are quite scattered. I found this thread and references therein. By that thread, the solution here would be to use a generator.

The devil’s in the details

Now, the problem is, how do we initialize the generator and how do we retain, say G2, for the next yield?

  • 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-27T20:16:22+00:00Added an answer on May 27, 2026 at 8:16 pm

    Writing generators is a very good practice!

    import random 
    import math
    def BoxMullerFormula(a,b):
        c = math.sqrt(-2 * math.log(a)) * math.cos(2 * math.pi * b)
        d = math.sqrt(-2 * math.log(a)) * math.sin(2 * math.pi * b)
        return (c,d)
    def BoxM(): 
         while 1:
            Rand01 = random.random()
            Rand02 = random.random()
            (a,b) = BoxMullerFormula(Rand01,Rand02)
            yield a 
            yield b 
    
    BoxM01 = BoxM()         
    for i in xrange(10):
        print BoxM01.next() 
    

    You should consider also having a class BoxMuller with a get() method, a re_initialize() one, and so on.

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

Sidebar

Related Questions

say, I want to use an existing SqlCe Database which HAS data in it.
I've had this problem a couple of times. Let's say I want to display
Let's say I want to generate this output: public String toString() { return this.getFirstName()
What's a common way of generating sentences from a grammar? I want an algorithm
Say the use scala to process incoming email etc. In what context is (or
Say I have a web service where I want to serve the content only
Let's say I: malloc a pthread_t for holding a thread context pthread_create with as
I want to use ImageJ to do some processing of several thousand images. Is
The scenario Let's say I have a sidebar where every box / widget is
I'm looking for a way to use the ouput of a command (say command1)

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.