Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Random No Generation

Reply
Thread Tools

Random No Generation

 
 
John E
Guest
Posts: n/a
 
      12-23-2003
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.


 
Reply With Quote
 
 
 
 
Dario
Guest
Posts: n/a
 
      12-23-2003
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));
}

 
Reply With Quote
 
 
 
 
Brian Palmer
Guest
Posts: n/a
 
      12-23-2003
"John E" <La-la-> 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
 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      12-23-2003
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


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-23-2003
"John E" <La-la-> wrote in message
news:bs9606$an9bs$...
> 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


 
Reply With Quote
 
Alex Hunsley
Guest
Posts: n/a
 
      12-23-2003
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

 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      12-23-2003
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 ...
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-23-2003
On Tue, 23 Dec 2003 11:58:27 +0100, Dario <> wrote
or quoted :

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

nextInt works even better. See
http://mindprod.com/jgloss/randomnumbers.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      12-23-2003
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


 
Reply With Quote
 
Alex Hunsley
Guest
Posts: n/a
 
      12-24-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Random Number Generation dpi VHDL 4 03-26-2010 10:31 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random generation in function ALuPin VHDL 1 09-01-2004 06:15 PM
HTML Generation (Next Generation CGI) John W. Long Ruby 4 11-24-2003 04:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57