2017-05-25 28 views
1

我想用它来生成10个具有随机价格和容量的Udisk实例。带有随机种子的Java TreeMap没有固定大小

// test.java 
import java.util.*; 

class Udisk { 
    float price; 
    int capacity; 
    Udisk(float p, int c) { 
     this.price = p; 
     this.capacity = c; 
    } 
    @Override 
    public String toString() { 
     return "Udisk price: "+this.price+" capacity: "+this.capacity; 
    } 
} 

class Main { 
    public static void main(String[] args) { 
     Map<Float, Udisk> tm = new TreeMap<Float, Udisk>(); 
     for(int count=0; count < 10; count++) { 
      Random rand = new Random(System.currentTimeMillis()); 
      float price = Math.abs(rand.nextInt())%100; 
      int capacity = Math.abs(rand.nextInt())%10; 
      Udisk u = new Udisk(price, capacity); 
      tm.put(u.price, u); 
     } 
     System.out.println(tm+ " "+tm.size()); 
    } 
} 

javac test.java后,我跑了java Main多次,结果就是这么奇怪

First Time: {3.0=Udisk price: 3.0 capacity: 1} 1 
Second Time: {33.0=Udisk price: 33.0 capacity: 8, 86.0=Udisk price: 86.0 capacity: 0} 2 
Third Time: {46.0=Udisk price: 46.0 capacity: 8} 1 
Fourth Time: {24.0=Udisk price: 24.0 capacity: 1, 73.0=Udisk price: 73.0 capacity: 5} 2 

所有这些结果具有树形图少于10项。代码更改为

import java.util.*; 

class Udisk { 
    float price; 
    int capacity; 
    Udisk(float p, int c) { 
     this.price = p; 
     this.capacity = c; 
    } 
    @Override 
    public String toString() { 
     return "Udisk price: "+this.price+" capacity: "+this.capacity; 
    } 
} 

class Main { 
    public static void main(String[] args) { 
     Map<Float, Udisk> tm = new TreeMap<Float, Udisk>(); 
     for(int count=0; count < 10; count++) { 
      Random rand = new Random(System.currentTimeMillis()); 
      float price = Math.abs(count)%100; 
      int capacity = Math.abs(count)%10; 
      Udisk u = new Udisk(price, capacity); 
      tm.put(u.price, u); 
     } 
     System.out.println(tm+ " "+tm.size()); 
    } 
} 

后的答案是正确的

{0.0=Udisk price: 0.0 capacity: 0, 1.0=Udisk price: 1.0 capacity: 1, 
2.0=Udisk price: 2.0 capacity: 2, 3.0=Udisk price: 3.0 capacity: 3, 
4.0=Udisk price: 4.0 capacity: 4, 5.0=Udisk price: 5.0 capacity: 5, 
6.0=Udisk price: 6.0 capacity: 6, 7.0=Udisk price: 7.0 capacity: 7, 
8.0=Udisk price: 8.0 capacity: 8, 9.0=Udisk price: 9.0 capacity: 9} 10 

我不知道我是怎么错过了随机导致前者的问题,感谢您的帮助!

回答

1

这方面的一些想法:

一)初始化随机环内是昂贵的 - 你总是初始化一个新的随机伪随机数序列,而你也可以同样拉随机创造出循环和做

b)你们的价格整数模100 - >只有100可能的值,所以你可能会想到键冲突(在这种情况下,10次迭代不会产生10种不同元素)

,最重要的

c)System.currentTimeMillis()可能太慢了。你的循环只创建一个对象并将其放入地图 - 这非常非常快(即使是随机播种)。 这可能是因为System.currentTimeMillis()在迭代之间没有变化(并且您总是重新种子),您可能最终只需初始化具有相同种子值的伪随机序列!)。相同种子的兰德姆生成相同的序列。因此,除非您的System.currentTimeMillis()在迭代之间改变(并且在某些系统上这种情况只发生在每个〜15ms内),否则您最终只会得到一个或最多两个不同的价格值。

尝试拉动随机跳出循环:

Random rand = new Random(System.currentTimeMillis()); 
    for(int count=0; count < 10; count++) { 
     float price = Math.abs(rand.nextInt())%100; 
     int capacity = Math.abs(rand.nextInt())%10; 
     Udisk u = new Udisk(price, capacity); 
     tm.put(u.price, u); 
    } 

现在你应该更接近10倍的值。不过你可能会不吉利,并得出一致nextInt()值的两倍 - 如果你需要究竟 10个值我建议作出的循环条件:

Random rand = new Random(System.currentTimeMillis()); 
    while(tm.size() < 10) { 
     float price = Math.abs(rand.nextInt())%100; 
     int capacity = Math.abs(rand.nextInt())%10; 
     Udisk u = new Udisk(price, capacity); 
     tm.put(u.price, u); 
    } 

好运。

+0

非常感谢!这有助于很多:) –

+0

随时接受为答复/ upvote :-) – Jan