1--6之间随机数数概论!

来源:互联网 发布:网站数据库维护 编辑:程序博客网 时间:2024/05/18 01:31
package method;
import javax.swing.*;

//每次生成的数字都不相同
public class RollDie {
    
public static void main(String args[]){
        
int frequency1=0;
        
int frequency2=0;
        
int frequency3=0;
        
int frequency4=0;
        
int frequency5=0;
        
int frequency6=0;
        
int face;
        
        
for (int i=1;i<=6000;i++){
            face 
= 1 + (int)(Math.random()*6);
            
switch(face){
            
case 1:
                
++frequency1;
                
break;
            
case 2:
                
++frequency2;
                
break;
            
case 3:
                
++frequency3;
                
break;
            
case 4:
                
++frequency4;
                
break;
            
case 5:
                
++frequency5;
                
break;
            
case 6:
                
++frequency6;
                
break;
            }

        }

        JTextArea outputArea 
= new JTextArea();
        outputArea.setText(
"Face Frequency"+" 1 "+frequency1+
                        
" 2 "+frequency2+" 3 "+frequency3+
                        
" 4 "+frequency4+" 5 "+frequency5+
                        
" 6 "+frequency6);
        
        JOptionPane.showMessageDialog(
null, outputArea,"Rolling a Die 6000 Times",JOptionPane.INFORMATION_MESSAGE);
        System.exit(
0);
    }

}

 1---6之间每个整数都应该出现1000次左右!!

face = 1 + ( int ) ( Math.random() * 6 );

数 =  一位值 + ( Math.random() * 缩放因子 );