Topic: 关于多线程的一道问题!

  Print this page

1.关于多线程的一道问题! Copy to clipboard
Posted by: zerog
Posted on: 2006-08-23 10:51

“编程生产100个1到6之间的随机数,统计1到6每个数出现的概率。
并且在三个线程中各生成100个1到6之间的随机数,最后统计每个数字出现的总概率。”
这个问题比较难,希望why不惜赐教一下,大虾们也帮解答一下这道题?

2.Re:关于多线程的一道问题! [Re: zerog] Copy to clipboard
Posted by: why
Posted on: 2006-08-23 19:41

Please write code first.
Write the method that 生产1到6之间的随机数
then write the method that 统计每个数字出现
if you have problem with 线程, let us know then
but write code first!

3.Re:关于多线程的一道问题! [Re: zerog] Copy to clipboard
Posted by: Stub
Posted on: 2006-08-23 20:02

下面,是在下的试着写出的程序。在下也是个JAVA初学者,虽然有很多问题解绝不了,但这个问题还是很简单的。LZ请在自己试着写过程序之后,在来看看在下的代码。Don't be afraid to try.

欢迎对在下的代码提提意见^_^

public class TestRandomMath extends Thread
{
public static void main (String [] argv)
{
TestRandomMath t1 = new TestRandomMath();
TestRandomMath t2 = new TestRandomMath();
TestRandomMath t3 = new TestRandomMath();
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.setPriority(Thread.MAX_PRIORITY);
t2.start();
t3.setPriority(Thread.MAX_PRIORITY);
t3.start();
}

private int random, one, two, three, four, five, six;

public void run()
{
for (int i = 0; i < 100; i++)
{
random = (int) (Math.random()*6+1);
switch (random)
{
case 1:
one = ++ one;
System.out.print(1);
break;
case 2:
two = ++ two;
System.out.print(2);
break;
case 3:
three = ++ three;
System.out.print(3);
break;
case 4:
four = ++ four;
System.out.print(4);
break;
case 5:
five = ++ five;
System.out.print(5);
break;
case 6:
six = ++ six;
System.out.print(6);
break;
}
}

System.out.println();
int sum = one+two+three+four+five+six;
System.out.println("总共得到了"+sum+"个");
System.out.println("出现1的概率为"+one+"%");
System.out.println("出现2的概率为"+two+"%");
System.out.println("出现3的概率为"+three+"%");
System.out.println("出现4的概率为"+four+"%");
System.out.println("出现5的概率为"+five+"%");
System.out.println("出现6的概率为"+six+"%");

}

}

4.Re:关于多线程的一道问题! [Re: Stub] Copy to clipboard
Posted by: colo007
Posted on: 2006-08-23 20:44

Stub wrote:
random = (int) (Math.random()*6+1);

我提出自己的小疑问,是否这里的*应该换成 % ?
呵呵?

5.Re:关于多线程的一道问题! [Re: colo007] Copy to clipboard
Posted by: why
Posted on: 2006-08-24 02:08

colo007 wrote:
我提出自己的小疑问,是否这里的*应该换成 % ?

Why do you think so?
0.0 <= Math.random() < 1.0

6.Re:关于多线程的一道问题! [Re: zerog] Copy to clipboard
Posted by: d_ai_w_ei
Posted on: 2006-08-26 16:33

如果将"one = ++ one"写成"++one"是否更清晰些呢?

7.Re:关于多线程的一道问题! [Re: zerog] Copy to clipboard
Posted by: zerog
Posted on: 2006-08-30 23:29

非常感谢大虾们的解答,最近特忙忘记跟大家说声谢谢了,我把这道题我写的代码放上来,欢迎大家对我写的提意见,还有我是个初学者,不知道怎么背景是黑色的写代码,我都是用eclipse来写的,有谁能告诉我是怎么弄的吗?

public class text {
public static void main(String[] args) throws InterruptedException {
Data data = new Data();
Stat stat = new Stat();
stat.setMap(data.getMap());
stat.start();
stat.join();
data.print();
}
}

class Stat extends Thread {
public Stat() {}
public Stat(String s) {super(s);}
public Stat(ThreadGroup group, String s) {super(group, s);}
private Map<Integer, Double> map;
public void setMap(Map<Integer, Double> map) {
this.map = map;
}

public void run() {
for(int i = 0; i < 100; i++) {
Integer number = new Integer(Math.random(1, 6));
if(map.containsKey(number))
map.put(number, new Double(map.get(number).intValue() + 1));
else
map.put(number, new Double(1));
}
}
}

class Data {
private Map<Integer, Double> map = Collections.synchronizedMap(new HashMap<Integer, Double>());
public Map<Integer, Double> getMap() {
return map;
}

public void print() {
for(int i = 1; i < 7; i++){
map.put(new Integer(i), new Double(map.get(new Integer(i)).doubleValue() / 300));
}
System.out.println(map);
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923