seeding random numbers

Discussion in 'Java' started by (-Peter-), Feb 20, 2008.

  1. (-Peter-)

    (-Peter-) Guest

    Hi..

    I'm programming a Monte Carlo problem where i need to show that my
    results are reproducible - that is I need to prove this by running by
    program with seeded random numbers.

    The program is build up of a main method and a lot of other sub-
    routines. How can I create a random number generator, from which I can
    get my seeded random numbers. It shall be possible to get one random
    number at a time a lot times (some millions), and I need to call the
    random number generator from any of the subroutines and get the same
    sequence of numbers no matter from which routines i call them (also
    shifting between the sub-routines for example 5 calls from routine 1
    followed by 5 from the main routine should give the same numbers as if
    I called the generator 4 times from the main routine followed by 5
    times from routine number 10 followed by one call from the main
    routine again)

    I hope the description can be understood - it is a bit difficult to
    explain..

    Can anybody help me to solve this?

    /Peter
    (-Peter-), Feb 20, 2008
    #1
    1. Advertising

  2. (-Peter-)

    Eric Sosman Guest

    (-Peter-) wrote:
    > Hi..
    >
    > I'm programming a Monte Carlo problem where i need to show that my
    > results are reproducible - that is I need to prove this by running by
    > program with seeded random numbers.
    >
    > The program is build up of a main method and a lot of other sub-
    > routines. How can I create a random number generator, from which I can
    > get my seeded random numbers. It shall be possible to get one random
    > number at a time a lot times (some millions), and I need to call the
    > random number generator from any of the subroutines and get the same
    > sequence of numbers no matter from which routines i call them (also
    > shifting between the sub-routines for example 5 calls from routine 1
    > followed by 5 from the main routine should give the same numbers as if
    > I called the generator 4 times from the main routine followed by 5
    > times from routine number 10 followed by one call from the main
    > routine again)


    Create as many different java.util.Random objects as you
    need, using the same seed value for each:

    long seed = 123456789; // or whatever
    Random rmain = new Random(seed);
    Random rsub1 = new Random(seed);
    ...

    Devote one Random object to each "subroutine" or group of
    related subroutines, so each "subroutine" obtains values only
    from its own Random object.

    ... but I may have misunderstood just what you mean by "the
    same sequence." If it seems I have, please try to explain it
    more clearly.

    --
    Eric Sosman, Feb 20, 2008
    #2
    1. Advertising

  3. (-Peter-)

    (-Peter-) Guest

    On 20 Feb., 22:34, Eric Sosman <> wrote:
    > (-Peter-) wrote:
    > > Hi..

    >
    > > I'm programming a Monte Carlo problem where i need to show that my
    > > results are reproducible - that is I need to prove this by running by
    > > program with seeded random numbers.

    >
    > > The program is build up of a main method and a lot of other sub-
    > > routines. How can I create a random number generator, from which I can
    > > get my seeded random numbers. It shall be possible to get one random
    > > number at a time a lot times (some millions), and I need to call the
    > > random number generator from any of the subroutines and get the same
    > > sequence of numbers no matter from which routines i call them (also
    > > shifting between the sub-routines for example 5 calls from routine 1
    > > followed by 5 from the main routine should give the same numbers as if
    > > I called the generator 4 times from the main routine followed by 5
    > > times from routine number 10 followed by one call from the main
    > > routine again)

    >
    > Create as many different java.util.Random objects as you
    > need, using the same seed value for each:
    >
    > long seed = 123456789; // or whatever
    > Random rmain = new Random(seed);
    > Random rsub1 = new Random(seed);
    > ...
    >
    > Devote one Random object to each "subroutine" or group of
    > related subroutines, so each "subroutine" obtains values only
    > from its own Random object.
    >
    > ... but I may have misunderstood just what you mean by "the
    > same sequence." If it seems I have, please try to explain it
    > more clearly.
    >
    > --
    >


    I think you have misunderstood it (because of the bad explanation)..

    Generally I need ONE sequence (therefore I think I do only need one
    generator) of random numbers for example (which are seeded, so I get
    the same results every time I run the code)

    0.21 0.23 0.1 0.9 0.5 0.7

    I will when use the first number (in this example 0.21) to decide
    which sub-routine should be run. This sub-routine (call this sub1)
    uses the next random number (0.23) and when "decides" to go to another
    sub-routine (sub2) which uses the random number (0.1)...

    If I seeded the random number generator another way I would for
    example get these numbers:

    0.7 0.1 0.21 0.81 0.21 0.001

    The first number (0.7) is now calling another subroutine(sub4) which
    uses the number 0.1 to decide to go back to the main method which uses
    the random number (0.21). This calls sub2 which uses 0.81....

    That is no matter what the numbers are I will use them in them in the
    same order - which numbers are used in which method is "random".

    I hope this was a better explanation.. If not please tell me..

    /Peter
    (-Peter-), Feb 20, 2008
    #3
  4. (-Peter-)

    Eric Sosman Guest

    (-Peter-) wrote:
    >
    > Generally I need ONE sequence (therefore I think I do only need one
    > generator) of random numbers for example (which are seeded, so I get
    > the same results every time I run the code)
    >
    > 0.21 0.23 0.1 0.9 0.5 0.7
    >
    > I will when use the first number (in this example 0.21) to decide
    > which sub-routine should be run. This sub-routine (call this sub1)
    > uses the next random number (0.23) and when "decides" to go to another
    > sub-routine (sub2) which uses the random number (0.1)...
    >
    > If I seeded the random number generator another way I would for
    > example get these numbers:
    >
    > 0.7 0.1 0.21 0.81 0.21 0.001
    >
    > The first number (0.7) is now calling another subroutine(sub4) which
    > uses the number 0.1 to decide to go back to the main method which uses
    > the random number (0.21). This calls sub2 which uses 0.81....
    >
    > That is no matter what the numbers are I will use them in them in the
    > same order - which numbers are used in which method is "random".


    I'm sorry, but I still don't understand your difficulty.
    If you create two Random objects using the same seed value,
    and if you make the same sequence of method calls on each,
    they will deliver identical result streams. So if you run
    your program using a Random whose seed is 123456789, and then
    you run the program it a second time and again use the seed
    value 123456789, you will get the same pseudo-random values
    in both executions.

    --
    Eric Sosman, Feb 20, 2008
    #4
  5. (-Peter-)

    Christian Guest

    (-Peter-) schrieb:
    > On 20 Feb., 22:34, Eric Sosman <> wrote:
    >> (-Peter-) wrote:
    >>> Hi..
    >>> I'm programming a Monte Carlo problem where i need to show that my
    >>> results are reproducible - that is I need to prove this by running by
    >>> program with seeded random numbers.
    >>> The program is build up of a main method and a lot of other sub-
    >>> routines. How can I create a random number generator, from which I can
    >>> get my seeded random numbers. It shall be possible to get one random
    >>> number at a time a lot times (some millions), and I need to call the
    >>> random number generator from any of the subroutines and get the same
    >>> sequence of numbers no matter from which routines i call them (also
    >>> shifting between the sub-routines for example 5 calls from routine 1
    >>> followed by 5 from the main routine should give the same numbers as if
    >>> I called the generator 4 times from the main routine followed by 5
    >>> times from routine number 10 followed by one call from the main
    >>> routine again)

    >> Create as many different java.util.Random objects as you
    >> need, using the same seed value for each:
    >>
    >> long seed = 123456789; // or whatever
    >> Random rmain = new Random(seed);
    >> Random rsub1 = new Random(seed);
    >> ...
    >>
    >> Devote one Random object to each "subroutine" or group of
    >> related subroutines, so each "subroutine" obtains values only
    >> from its own Random object.
    >>
    >> ... but I may have misunderstood just what you mean by "the
    >> same sequence." If it seems I have, please try to explain it
    >> more clearly.
    >>
    >> --
    >>

    >
    > I think you have misunderstood it (because of the bad explanation)..
    >
    > Generally I need ONE sequence (therefore I think I do only need one
    > generator) of random numbers for example (which are seeded, so I get
    > the same results every time I run the code)
    >
    > 0.21 0.23 0.1 0.9 0.5 0.7
    >
    > I will when use the first number (in this example 0.21) to decide
    > which sub-routine should be run. This sub-routine (call this sub1)
    > uses the next random number (0.23) and when "decides" to go to another
    > sub-routine (sub2) which uses the random number (0.1)...
    >
    > If I seeded the random number generator another way I would for
    > example get these numbers:
    >
    > 0.7 0.1 0.21 0.81 0.21 0.001
    >
    > The first number (0.7) is now calling another subroutine(sub4) which
    > uses the number 0.1 to decide to go back to the main method which uses
    > the random number (0.21). This calls sub2 which uses 0.81....
    >
    > That is no matter what the numbers are I will use them in them in the
    > same order - which numbers are used in which method is "random".
    >
    > I hope this was a better explanation.. If not please tell me..
    >
    > /Peter
    >
    >

    It seems to me that it would fit for you to create a single Random
    object. Make it accessible to everywhere from the program.
    i.e. using static or create a singleton to hold it.

    Christian
    Christian, Feb 20, 2008
    #5
  6. (-Peter-) wrote:
    ....
    > Generally I need ONE sequence (therefore I think I do only need one
    > generator) of random numbers for example (which are seeded, so I get
    > the same results every time I run the code)


    In that case, construct a single instance of java.util.Random,
    specifying the seed on the constructor call, and use it throughout the
    program.

    Patricia
    Patricia Shanahan, Feb 20, 2008
    #6
  7. (-Peter-)

    Jason Cavett Guest

    On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:
    > (-Peter-) wrote:
    >
    > ...
    >
    > > Generally I need ONE sequence (therefore I think I do only need one
    > > generator) of random numbers for example (which are seeded, so I get
    > > the same results every time I run the code)

    >
    > In that case, construct a single instance of java.util.Random,
    > specifying the seed on the constructor call, and use it throughout the
    > program.
    >
    > Patricia


    Patricia is right - a single instance across the program is the way to
    go. HOWEVER...

    The only downside to using java.util.Random is that it tends not to be
    "random" enough. So, if you're interested in a "more random" answer,
    check into the Mersenne Twister. (There's a good article on
    Wikipedia.)
    Jason Cavett, Feb 21, 2008
    #7
  8. (-Peter-)

    Roedy Green Guest

    On Wed, 20 Feb 2008 13:24:51 -0800 (PST), "(-Peter-)"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I'm programming a Monte Carlo problem where i need to show that my
    >results are reproducible - that is I need to prove this by running by
    >program with seeded random numbers.


    see http://mindprod.com/jgloss/pseudorandom.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Feb 21, 2008
    #8
  9. (-Peter-)

    Roedy Green Guest

    On Wed, 20 Feb 2008 14:54:08 -0800, Patricia Shanahan <>
    wrote, quoted or indirectly quoted someone who said :

    >In that case, construct a single instance of java.util.Random,
    >specifying the seed on the constructor call, and use it throughout the
    >program.


    If you have threads this won't work. You would need one generator per
    thread to get reproducible results. One generator will suffice so
    long as you make calls on it in the same order each time for each
    sample you want on a single thread.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Feb 21, 2008
    #9
  10. (-Peter-)

    (-Peter-) Guest

    On 21 Feb., 02:23, Jason Cavett <> wrote:
    > On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:
    >
    > > (-Peter-) wrote:

    >
    > > ...

    >
    > > > Generally I need ONE sequence (therefore I think I do only need one
    > > > generator) of random numbers for example (which are seeded, so I get
    > > > the same results every time I run the code)

    >
    > > In that case, construct a single instance of java.util.Random,
    > > specifying the seed on the constructor call, and use it throughout the
    > > program.

    >
    > > Patricia

    >
    > Patricia is right - a single instance across the program is the way to
    > go.  HOWEVER...
    >
    > The only downside to using java.util.Random is that it tends not to be
    > "random" enough.  So, if you're interested in a "more random" answer,
    > check into the Mersenne Twister.  (There's a good article on
    > Wikipedia.)


    Okay I will look at that..

    How do I create this object?

    How would the object know which of the numbers it shall use?

    I can not get a idea to construct it..

    please help med :)

    /Peter
    (-Peter-), Feb 21, 2008
    #10
  11. (-Peter-)

    (-Peter-) Guest

    On 21 Feb., 03:00, Roedy Green <>
    wrote:
    > On Wed, 20 Feb 2008 13:24:51 -0800 (PST), "(-Peter-)"
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    > >I'm programming a Monte Carlo problem where i need to show that my
    > >results are reproducible - that is I need to prove this by running by
    > >program with seeded random numbers.

    >
    > seehttp://mindprod.com/jgloss/pseudorandom.html
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    thank you for the link - it looks interesting..

    /peter
    (-Peter-), Feb 21, 2008
    #11
  12. (-Peter-)

    (-Peter-) Guest

    On 21 Feb., 08:59, "(-Peter-)" <> wrote:
    > On 21 Feb., 02:23, Jason Cavett <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:

    >
    > > > (-Peter-) wrote:

    >
    > > > ...

    >
    > > > > Generally I need ONE sequence (therefore I think I do only need one
    > > > > generator) of random numbers for example (which are seeded, so I get
    > > > > the same results every time I run the code)

    >
    > > > In that case, construct a single instance of java.util.Random,
    > > > specifying the seed on the constructor call, and use it throughout the
    > > > program.

    >
    > > > Patricia

    >
    > > Patricia is right - a single instance across the program is the way to
    > > go.  HOWEVER...

    >
    > > The only downside to using java.util.Random is that it tends not to be
    > > "random" enough.  So, if you're interested in a "more random" answer,
    > > check into the Mersenne Twister.  (There's a good article on
    > > Wikipedia.)

    >
    > Okay I will look at that..
    >
    > How do I create this object?
    >
    > How would the object know which of the numbers it shall use?
    >
    > I can not get a idea to construct it..
    >
    > please help med :)
    >
    > /Peter- Skjul tekst i anførselstegn -
    >
    > - Vis tekst i anførselstegn -


    I forgot one thing:

    I got one idea: Creating some million numbers and saving it in a
    array.. but I think that would be waste of memory!

    /Peter
    (-Peter-), Feb 21, 2008
    #12
  13. (-Peter-) wrote:
    > On 21 Feb., 02:23, Jason Cavett <> wrote:
    >> On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:
    >>
    >>> (-Peter-) wrote:
    >>> ...
    >>>> Generally I need ONE sequence (therefore I think I do only need one
    >>>> generator) of random numbers for example (which are seeded, so I get
    >>>> the same results every time I run the code)
    >>> In that case, construct a single instance of java.util.Random,
    >>> specifying the seed on the constructor call, and use it throughout the
    >>> program.
    >>> Patricia

    >> Patricia is right - a single instance across the program is the way to
    >> go. HOWEVER...
    >>
    >> The only downside to using java.util.Random is that it tends not to be
    >> "random" enough. So, if you're interested in a "more random" answer,
    >> check into the Mersenne Twister. (There's a good article on
    >> Wikipedia.)

    >
    > Okay I will look at that..
    >
    > How do I create this object?


    import java.util.Random;

    ....

    Random randomNumberSource = new Random(someSeed);

    [On the right hand side, you can substitute the constructor for any
    class that extends java.util.Random, if you prefer a better quality
    generator.]

    >
    > How would the object know which of the numbers it shall use?


    See the java.util.Random API documentation.

    >
    > I can not get a idea to construct it..


    Just like any other Java object. I'm not sure I'm seeing the difficulty
    here.

    Patricia
    Patricia Shanahan, Feb 21, 2008
    #13
  14. (-Peter-)

    (-Peter-) Guest

    On 21 Feb., 14:53, Patricia Shanahan <> wrote:
    > (-Peter-) wrote:
    > > On 21 Feb., 02:23, Jason Cavett <> wrote:
    > >> On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:

    >
    > >>> (-Peter-) wrote:
    > >>> ...
    > >>>> Generally I need ONE sequence (therefore I think I do only need one
    > >>>> generator) of random numbers for example (which are seeded, so I get
    > >>>> the same results every time I run the code)
    > >>> In that case, construct a single instance of java.util.Random,
    > >>> specifying the seed on the constructor call, and use it throughout the
    > >>> program.
    > >>> Patricia
    > >> Patricia is right - a single instance across the program is the way to
    > >> go.  HOWEVER...

    >
    > >> The only downside to using java.util.Random is that it tends not to be
    > >> "random" enough.  So, if you're interested in a "more random" answer,
    > >> check into the Mersenne Twister.  (There's a good article on
    > >> Wikipedia.)

    >
    > > Okay I will look at that..

    >
    > > How do I create this object?

    >
    > import java.util.Random;
    >
    > ...
    >
    > Random randomNumberSource = new Random(someSeed);
    >
    > [On the right hand side, you can substitute the constructor for any
    > class that extends java.util.Random, if you prefer a better quality
    > generator.]
    >
    >
    >
    > > How would the object know which of the numbers it shall use?

    >
    > See the java.util.Random API documentation.
    >
    >
    >
    > > I can not get a idea to construct it..

    >
    > Just like any other Java object. I'm not sure I'm seeing the difficulty
    > here.
    >
    > Patricia- Skjul tekst i anførselstegn -
    >
    > - Vis tekst i anførselstegn -


    I'll try to work on it again..

    thank you for your help.
    (-Peter-), Feb 21, 2008
    #14
  15. (-Peter-)

    Jason Cavett Guest

    On Feb 21, 3:05 am, "(-Peter-)" <> wrote:
    > On 21 Feb., 08:59, "(-Peter-)" <> wrote:
    >
    >
    >
    > > On 21 Feb., 02:23, Jason Cavett <> wrote:

    >
    > > > On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:

    >
    > > > > (-Peter-) wrote:

    >
    > > > > ...

    >
    > > > > > Generally I need ONE sequence (therefore I think I do only need one
    > > > > > generator) of random numbers for example (which are seeded, so I get
    > > > > > the same results every time I run the code)

    >
    > > > > In that case, construct a single instance of java.util.Random,
    > > > > specifying the seed on the constructor call, and use it throughout the
    > > > > program.

    >
    > > > > Patricia

    >
    > > > Patricia is right - a single instance across the program is the way to
    > > > go. HOWEVER...

    >
    > > > The only downside to using java.util.Random is that it tends not to be
    > > > "random" enough. So, if you're interested in a "more random" answer,
    > > > check into the Mersenne Twister. (There's a good article on
    > > > Wikipedia.)

    >
    > > Okay I will look at that..

    >
    > > How do I create this object?

    >
    > > How would the object know which of the numbers it shall use?

    >
    > > I can not get a idea to construct it..

    >
    > > please help med :)

    >
    > > /Peter- Skjul tekst i anførselstegn -

    >
    > > - Vis tekst i anførselstegn -

    >
    > I forgot one thing:
    >
    > I got one idea: Creating some million numbers and saving it in a
    > array.. but I think that would be waste of memory!
    >
    > /Peter


    That would definitely not be a good solution to this problem.
    (Besides, in a Monte Carlo simulation, it is quite possible that you
    could need more than a million numbers. What do you do then?)

    As Patricia said - making your class static would be the way to go.
    Of course, Roedy correctly points out that if you have a multi-
    threaded application, this solution is not guaranteed to work unless
    you can guarantee the order that the threads are grabbing the random
    numbers.
    Jason Cavett, Feb 21, 2008
    #15
  16. (-Peter-)

    Jason Cavett Guest

    On Feb 21, 2:59 am, "(-Peter-)" <> wrote:
    > On 21 Feb., 02:23, Jason Cavett <> wrote:
    >
    >
    >
    > > On Feb 20, 5:54 pm, Patricia Shanahan <> wrote:

    >
    > > > (-Peter-) wrote:

    >
    > > > ...

    >
    > > > > Generally I need ONE sequence (therefore I think I do only need one
    > > > > generator) of random numbers for example (which are seeded, so I get
    > > > > the same results every time I run the code)

    >
    > > > In that case, construct a single instance of java.util.Random,
    > > > specifying the seed on the constructor call, and use it throughout the
    > > > program.

    >
    > > > Patricia

    >
    > > Patricia is right - a single instance across the program is the way to
    > > go. HOWEVER...

    >
    > > The only downside to using java.util.Random is that it tends not to be
    > > "random" enough. So, if you're interested in a "more random" answer,
    > > check into the Mersenne Twister. (There's a good article on
    > > Wikipedia.)

    >
    > Okay I will look at that..
    >
    > How do I create this object?
    >
    > How would the object know which of the numbers it shall use?
    >
    > I can not get a idea to construct it..
    >
    > please help med :)
    >
    > /Peter


    You create the object like any normal Java object.

    Blah var = new Blah();

    You need to use the Mersenne Twister code (there is some found at
    Wikipedia) along with your own code.
    Jason Cavett, Feb 21, 2008
    #16
  17. (-Peter-)

    Roedy Green Guest

    On Thu, 21 Feb 2008 00:00:19 -0800 (PST), "(-Peter-)"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >> seehttp://mindprod.com/jgloss/pseudorandom.html


    why did the space after "see" disappear? I see this happening often.
    My original has an ordinary hex 20 space.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Feb 21, 2008
    #17
  18. (-Peter-)

    Roedy Green Guest

    On Wed, 20 Feb 2008 17:23:49 -0800 (PST), Jason Cavett
    <> wrote, quoted or indirectly quoted someone
    who said :

    >The only downside to using java.util.Random is that it tends not to be
    >"random" enough. So, if you're interested in a "more random" answer,
    >check into the Mersenne Twister. (There's a good article on
    >Wikipedia.)


    there is also SecureRandom. See
    http://mindprod.com/jgloss/pseudorandom.html for links to both.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Feb 21, 2008
    #18
  19. Roedy Green wrote:
    > On Thu, 21 Feb 2008 00:00:19 -0800 (PST), "(-Peter-)"
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >>> seehttp://mindprod.com/jgloss/pseudorandom.html

    >
    > why did the space after "see" disappear? I see this happening often.
    > My original has an ordinary hex 20 space.


    The editor for Google Groups is as buggy as OS/360 Release 1.

    --
    John W. Kennedy
    "The whole modern world has divided itself into Conservatives and
    Progressives. The business of Progressives is to go on making mistakes.
    The business of the Conservatives is to prevent the mistakes from being
    corrected."
    -- G. K. Chesterton
    John W. Kennedy, Feb 22, 2008
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe
    Replies:
    2
    Views:
    485
    Howard
    Nov 19, 2004
  2. Random Seeding

    , May 15, 2006, in forum: C Programming
    Replies:
    13
    Views:
    615
    Eric Sosman
    May 16, 2006
  3. Jack
    Replies:
    4
    Views:
    415
  4. HumanJHawkins
    Replies:
    2
    Views:
    499
    peter koch
    Nov 30, 2006
  5. Replies:
    3
    Views:
    555
    JimLewis
    May 7, 2009
Loading...

Share This Page