# Random No Generation

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

1. ### John EGuest

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

2. ### DarioGuest

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

3. ### Brian PalmerGuest

"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
4. ### Filip LarsenGuest

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
5. ### Andrew ThompsonGuest

"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
6. ### Alex HunsleyGuest

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
7. ### Thomas SchodtGuest

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
8. ### Roedy GreenGuest

Roedy Green, Dec 23, 2003
9. ### Filip LarsenGuest

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.

> 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
10. ### Alex HunsleyGuest

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.
>>>
>>>

>
>
>
>
>>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