This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A thorny problem about class Random  RSS feed

 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first please take a look at the follow code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class RandomTest{
public static void main(String [] args){
boolean[] bl = new boolean[20];
for(int i=0;i<bl.length;i++){
bl[i] = new java.util.Random();
}
for(int i=0;i<bl.length;i++){
System.out.println(bl[i]);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if you run the RandomTest.class,you will find the output are constantly the same everytime you run it, seems like every member of array bl is filled with the same value.
But if you modify the above code into the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class RandomTest{
public static void main(String [] args){
boolean[] bl = new boolean[20];
java.util.Random rand = new java.util.Random();
for(int i=0;i<bl.length;i++){
bl[i] = rand.nextBoolean();
}
for(int i=0;i<bl.length;i++){
System.out.println(bl[i]);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
when you manage to run the enhanced version of RandomTest,you will be satisfied outcome,which will give you 20 randomly generated boolean values everytime you run it.but what drove me crazy is the reason that makes a significantly differences of the output between the two versions of RandomTest. Anticipating your detailed explanation.THX!
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Random class uses a seed to generate random numbers. The random numbers are generated with this seed as the base.
When you say new Random(), it uses the current system time (in milliseconds) as the seed.
In your first version, the Random() is initialized to the same seed everytime, since there is not much time taken for the processing (and so the system time is same in every iteration).
From the API

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

As you can see, the instances will return same value always if they are created with the same seed. That is why you are getting the same values again and again.
 
Peng Fan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does a 48-bit seed mean and what's the linear congruential formula,what does the sequences of numbers refer to? if you felt i asked too much, i just have to say sorry, but,what a pity that i must beg your detailed explanation anyway.THX!
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fanny pan:
what does a 48-bit seed mean and what's the linear congruential formula,what does the sequences of numbers refer to? if you felt i asked too much, i just have to say sorry, but,what a pity that i must beg your detailed explanation anyway.THX!

The linear congruetial formula looks like this: x_i+1 = (fact * x_i + const) mod modul where fact, const and modul are constants that satisfy certain constraints.
As you can see, you generate the next random number with a deterministic formula from the preceding number. You start with x_0, which is the seed.
HTH
Stefan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!