Random No Generation

Discussion in 'Java' started by John E, Dec 23, 2003.

  1. John E

    John E Guest

    Can someone tell me *two* different ways of randomly generating integers
    between 1 and 3, which will produce *different* results even when run
    simultaneously?

    I've been using this (it's not very good though):

    private int getRandomNumber(int max)
    {
    Random rand = new Random();
    return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    }

    I need to act upon the results of this generator with more random numbers,
    but unfortunately it seems to produce the same results the second time
    therefore losing any element of apparent randomness.

    TIA.
     
    John E, Dec 23, 2003
    #1
    1. Advertising

  2. John E

    Dario Guest

    John E wrote:

    > private int getRandomNumber(int max)
    > {
    > Random rand = new Random();
    > return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    > }


    final Random rand = new random();
    private int getRandomNumber(int max)
    {
    return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    }
     
    Dario, Dec 23, 2003
    #2
    1. Advertising

  3. John E

    Brian Palmer Guest

    "John E" <> writes:

    > Can someone tell me *two* different ways of randomly generating integers
    > between 1 and 3, which will produce *different* results even when run
    > simultaneously?
    >
    > I've been using this (it's not very good though):
    >
    > private int getRandomNumber(int max)
    > {
    > Random rand = new Random();
    > return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    > }


    First, there's no reason to do this. The Random creators went to some
    work to provide a function to provide uniform values less than an int.
    Create and store a reference to a Random in the class:

    Random rand = new Random();
    private int getRandomNumber(int max) { return 1+rand.nextInt(3); }

    What is max supposed to do?

    > I need to act upon the results of this generator with more random numbers,
    > but unfortunately it seems to produce the same results the second time
    > therefore losing any element of apparent randomness.


    It's not clear to me exactly what you're trying to do. How do you know
    it produces the same results? (Note that with only 3 output values,
    I wouldn't be surprised to see similar-looking sequences).

    --
    See comp.lang.java.announce for java-related announcements
     
    Brian Palmer, Dec 23, 2003
    #3
  4. John E

    Filip Larsen Guest

    John E wrote

    > Can someone tell me *two* different ways of randomly generating

    integers
    > between 1 and 3, which will produce *different* results even when run
    > simultaneously?


    Your requirements are a bit vague, but the following field and method
    snippet should do what you ask for:

    public class MyClass {

    private Random rng = new Random();

    private int getRandomNumber(int min, int max) {
    return rng.nextInt(max-min+1)+min;
    }
    }

    Notice that 'rng' is initialized only once and then used to generate a
    sequence of numbers.


    > I've been using this (it's not very good though):
    >
    > private int getRandomNumber(int max)
    > {
    > Random rand = new Random();
    > return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    > }
    >
    > I need to act upon the results of this generator with more random

    numbers,
    > but unfortunately it seems to produce the same results the second time
    > therefore losing any element of apparent randomness.


    This is not good for several reasons:

    - Calling new Random() is, as the documentation says, equivalent to new
    Random(System.currentTimeMillis()), which in your case means that
    'rand.nextInt()' will return the exact same number until the next clock
    tick.

    - Do not use modulus on Random.nextInt() to get an evenly distributed
    random number, use Random.nextInt(int) instead (see documentation for
    the reason why).


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Dec 23, 2003
    #4
  5. "John E" <> wrote in message
    news:bs9606$an9bs$-berlin.de...
    > Can someone tell me *two* different ways of randomly generating integers
    > between 1 and 3, which will produce *different* results even when run
    > simultaneously?
    >
    > I've been using this (it's not very good though):
    >
    > private int getRandomNumber(int max)
    > {
    > Random rand = new Random();
    > return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    > }


    Try this,..

    java.util.Random r = new Random();

    private int getRandomNumber()
    {
    return r.nextInt(); // with whatever transforms..
    }

    Using a single random number generator
    is the best way to produce a series of
    random numbers..

    HTH

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Dec 23, 2003
    #5
  6. John E

    Alex Hunsley Guest

    Filip Larsen wrote:

    >John E wrote
    >
    >
    >
    >>Can someone tell me *two* different ways of randomly generating
    >>
    >>

    >integers
    >
    >
    >>between 1 and 3, which will produce *different* results even when run
    >>simultaneously?
    >>
    >>

    >
    >Your requirements are a bit vague, but the following field and method
    >snippet should do what you ask for:
    >
    >public class MyClass {
    >
    > private Random rng = new Random();
    >
    > private int getRandomNumber(int min, int max) {
    > return rng.nextInt(max-min+1)+min;
    > }
    >}
    >
    >Notice that 'rng' is initialized only once and then used to generate a
    >sequence of numbers.
    >
    >
    >
    >
    >>I've been using this (it's not very good though):
    >>
    >> private int getRandomNumber(int max)
    >> {
    >> Random rand = new Random();
    >> return (int)(1 + (Math.abs(rand.nextInt()) % 3));
    >> }
    >>
    >>I need to act upon the results of this generator with more random
    >>
    >>

    >numbers,
    >
    >
    >>but unfortunately it seems to produce the same results the second time
    >>therefore losing any element of apparent randomness.
    >>
    >>

    >
    >This is not good for several reasons:
    >
    >- Calling new Random() is, as the documentation says, equivalent to new
    >Random(System.currentTimeMillis()), which in your case means that
    >'rand.nextInt()' will return the exact same number until the next clock
    >tick.
    >
    >

    Not true. Any Random objects you *initialize* in the same clock tick
    will show the same deterministic sequence.
    Calling nextInt will always produce the next int, no matter if the clock
    has ticked forward since last time or not.
    It's the initialisation which is dependent (determined by) the current
    system time.

    >- Do not use modulus on Random.nextInt() to get an evenly distributed
    >random number, use Random.nextInt(int) instead (see documentation for
    >the reason why).
    >
    >

    Agreed, modulus results in a less random sequence in the end.

    alex
     
    Alex Hunsley, Dec 23, 2003
    #6
  7. Alex Hunsley <> wrote in
    news::

    > Filip Larsen wrote:
    >>- Calling new Random() is, as the documentation says, equivalent to new
    >>Random(System.currentTimeMillis()), which in your case means that
    >>'rand.nextInt()' will return the exact same number until the next clock
    >>tick.
    >>
    >>

    > Not true. Any Random objects you *initialize* in the same clock tick
    > will show the same deterministic sequence.
    > Calling nextInt will always produce the next int, no matter if the clock
    > has ticked forward since last time or not.
    > It's the initialisation which is dependent (determined by) the current
    > system time.


    I think he meant 'rand.nextInt()' *with a new instance of rand every call*
    will return ...
     
    Thomas Schodt, Dec 23, 2003
    #7
  8. John E

    Roedy Green Guest

    Roedy Green, Dec 23, 2003
    #8
  9. John E

    Filip Larsen Guest

    I wrote

    > >- Calling new Random() is, as the documentation says, equivalent to

    new
    > >Random(System.currentTimeMillis()), which in your case means that
    > >'rand.nextInt()' will return the exact same number until the next

    clock
    > >tick.


    and Alex Hunsley answered

    > Not true. Any Random objects you *initialize* in the same clock tick
    > will show the same deterministic sequence.
    > Calling nextInt will always produce the next int, no matter if the

    clock
    > has ticked forward since last time or not.
    > It's the initialisation which is dependent (determined by) the current
    > system time.


    Yes, I am not saying otherwise.

    Please note the words "in your case", by which I am refering to the code
    of the orignal poster. Here, all calls to the getRandomNumber method
    within same clock tick (i.e. within the period of time where
    System.currentTimeMillis() return the same value) will return the same
    number because 'rand' in the expression 'rand.nextInt()' refers to a new
    instance.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Dec 23, 2003
    #9
  10. John E

    Alex Hunsley Guest

    Filip Larsen wrote:

    >I wrote
    >
    >
    >
    >>>- Calling new Random() is, as the documentation says, equivalent to
    >>>
    >>>

    >new
    >
    >
    >>>Random(System.currentTimeMillis()), which in your case means that
    >>>'rand.nextInt()' will return the exact same number until the next
    >>>
    >>>

    >clock
    >
    >
    >>>tick.
    >>>
    >>>

    >
    >and Alex Hunsley answered
    >
    >
    >
    >>Not true. Any Random objects you *initialize* in the same clock tick
    >>will show the same deterministic sequence.
    >>Calling nextInt will always produce the next int, no matter if the
    >>
    >>

    >clock
    >
    >
    >>has ticked forward since last time or not.
    >>It's the initialisation which is dependent (determined by) the current
    >>system time.
    >>
    >>

    >
    >Yes, I am not saying otherwise.
    >
    >Please note the words "in your case", by which I am refering to the code
    >of the orignal poster. Here, all calls to the getRandomNumber method
    >within same clock tick (i.e. within the period of time where
    >System.currentTimeMillis() return the same value) will return the same
    >number because 'rand' in the expression 'rand.nextInt()' refers to a new
    >instance.
    >
    >
    >Regards,
    >
    >

    Oh, I see what you mean now! Ok.

    alex
     
    Alex Hunsley, Dec 24, 2003
    #10
    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. ALuPin

    Random generation in function

    ALuPin, Sep 1, 2004, in forum: VHDL
    Replies:
    1
    Views:
    661
    Ralf Hildebrandt
    Sep 1, 2004
  2. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,215
    vipinlal
    Mar 26, 2010
  3. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    395
    John W. Long
    Nov 24, 2003
  5. VK
    Replies:
    15
    Views:
    1,312
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page