Home arrow static arrow Java Programming [Archive] - more random
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - more random
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:114
Registered: 7/5/04
more random  
Aug 2, 2004 6:36 AM



 
Ihi

n my class I using Random instance, I notice that the random has strong inclination to choose the first numbers of its range. I'm not using any seed(and I don�t know what it is) . how can I make it mare randomly?

thanks
D
 

Posts:105
Registered: 6/8/04
Re: more random  
Aug 2, 2004 6:43 AM (reply 1 of 16)



 
How strong is this inclination you're finding? Based on how many trials?

You're probably just experiencing unlikely events. You can't increase the "randomness" of a random number generator.
 

Posts:5,965
Registered: 5/17/03
Re: more random  
Aug 2, 2004 6:50 AM (reply 2 of 16)



 
Maybe you're using nextGaussian?
 

Posts:114
Registered: 7/5/04
Re: more random  
Aug 2, 2004 12:00 PM (reply 3 of 16)



 
no im using nextint(5). Most of its functions it choose between 1 and 2.
 

Posts:37,103
Registered: 3/30/99
Re: more random  
Aug 2, 2004 12:01 PM (reply 4 of 16)



 
no im using nextint(5). Most of its functions it
choose between 1 and 2.

After 1000 calls, how many of each (0-4) were there?
 

Posts:37,103
Registered: 3/30/99
Re: more random  
Aug 2, 2004 12:01 PM (reply 5 of 16)



 
And you're only creating one instance of Random, right?
 

Posts:2,206
Registered: 8/15/02
Re: more random  
Aug 2, 2004 12:03 PM (reply 6 of 16)



 
And you're only creating one instance of Random,
right?

This is very important. Make sure Random is a class variable, bit local to a method.
 

Posts:19,725
Registered: 9/26/01
Re: more random  
Aug 2, 2004 12:04 PM (reply 7 of 16)



 
"more random" - pretty funny!
 

Posts:2,206
Registered: 8/15/02
Re: more random  
Aug 2, 2004 12:05 PM (reply 8 of 16)



 
Does Math.random() and Random() really generate random numbers, or are they just indeterminate?
 

Posts:37,103
Registered: 3/30/99
Re: more random  
Aug 2, 2004 12:17 PM (reply 9 of 16)



 
Does Math.random() and Random() really generate random
numbers, or are they just indeterminate?

Neither. If you seed the generator with the same number, you'll get the same sequence.
 

Posts:114
Registered: 7/5/04
Re: more random  
Aug 2, 2004 8:43 PM (reply 10 of 16)



 
And you're only creating one instance of Random,
right?

This is very important. Make sure Random is a
class variable, bit local to a method.

what yuo mean? in my class level :

 Random rnd = new Random();


should i declare only
Random rnd;
in class level and the
rnd=new Random() 
in the method level?
 

Posts:5,965
Registered: 5/17/03
Re: more random  
Aug 2, 2004 8:50 PM (reply 11 of 16)



 
Does Math.random() and Random() really generate random
numbers, or are they just indeterminate?

Random numbers cannot be generated using a computer. You get a determinate series of pseudorandom numbers.
 

Posts:5,965
Registered: 5/17/03
Re: more random  
Aug 2, 2004 8:55 PM (reply 12 of 16)



 
should i declare only
Random rnd;
in
class level and the
rnd=new Random() 
in
the method level?

It doesn't matter but you should instantiate Random once only and then make your 1000 calls to nextInt.
 

Posts:5,965
Registered: 5/17/03
Re: more random  
Aug 2, 2004 9:02 PM (reply 13 of 16)



 
It doesn't matter but you should instantiate Random
once only and then make your 1000 calls to nextInt.

The reason is that each time you instantiate Random (without a seed) it seeds itself from the system clock. But this clock has a resolution of 1 millisecond only. It's a common mistake to instantiate Random, call nextInt, then instantiate a new Random, call nextInt etcetera. If this is done within the same millisecond the seed will be the same and all calls to nextInt will give the same random number namely the first in the random series corresponding to that seed.

So use just one instantiation of Random and then make successive calls to nextInt.

In verson 1.5 Random no longer seeds from the system clock so I think this problem has disappeared.
 

Posts:12,831
Registered: 2/22/00
Re: more random  
Aug 2, 2004 9:16 PM (reply 14 of 16)



 

should i declare only
Random rnd;
in
class level and the
rnd=new Random() 
in
the method level?

Generally, no; you have it correct now.

You can instantiate a new Random object in a method, but if you do something like this:
private Thing getRandomThing() {    r = new Random();    return things[r.nextInt(things.length)];}

then call it too frequently (see UlrikaJ's post) you'll end up getting duplicate, non-random numbers.
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »