本文目錄一覽:
java中產生隨機數的函數是什麼
double
number=Math.random();
Math.random()是產生0到1的方法(不包括1)
如果想要產生0到100的隨機數可以寫成:
double
number=Math.random()*100;(想包含100的話寫成double
number=Math.floor(Math.random());)
Java怎麼產生隨機數?
一、利用random方法來生成隨機數。
在Java語言中生成隨機數相對來說比較簡單,因為有一個現成的方法可以使用。在Math類中,Java語言提供了一個叫做random的方法。通過這個方法可以讓系統產生隨機數。
二、通過Random類來生成隨機數。
在Java語言中,除了可以通過random 方法來產生隨機數之外,還可以通過一個random類來產生隨機數。程序開發人員可以通過實例化一個Random對象來創建一個隨機數的生成器。如 Random i=new Random()。通過這條語句就利用了Random類創建了一個隨機數的生成器。數
三、產生隨機的字元。
可以利用random方法來產生隨機字元。如可以利用代碼生成一個隨機的小寫字元:(char)(『a』+Math.random()*(『z』-『a』+1))。其實這跟生成任意兩個數之間的隨機數類似。通過以上的代碼就可以生成一個範圍之內的任意隨機字元。通過對這個代碼進行適當的修整,還可以生成任意兩個字元之間的隨機字元與任意大寫字元的隨機字元。其轉換的方式跟上面提到的任意範圍之內的隨機數類似。
下面來了解下隨機數的運用:
在統計學的不同技術中需要使用隨機數,比如在從統計總體中抽取有代表性的樣本的時候,或者在將實驗動物分配到不同的試驗組的過程中,或者在進行蒙特卡羅模擬法計算的時候等等。
真正的隨機數是使用物理現象產生的:比如擲錢幣、骰子、轉輪、使用電子元件的噪音、核裂變等等。這樣的隨機數發生器叫做物理性隨機數發生器,它們的缺點是技術要求比較高。
在實際應用中往往使用偽隨機數就足夠了。這些數列是「似乎」隨機的數,實際上它們是通過一個固定的、可以重複的計算方法產生的。計算機或計算器產生的隨機數有很長的周期性。它們不真正地隨機,因為它們實際上是可以計算出來的,但是它們具有類似於隨機數的統計特徵。這樣的發生器叫做偽隨機數發生器。
在真正關鍵性的應用中,比如在密碼學中,人們一般使用真正的隨機數。
C語言、C++、C#、Java、Matlab等程序語言和軟體中都有對應的隨機數生成函數,如rand等。
java的隨機函數怎麼用,請據個例子
今天做的一個一百以內加減乘除運算的程序,裡面就有隨即函數的使用的例子。
package justfortest;
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import student_docu_managerment_sys.Gridbaglayoutadd;
public class Caculater extends JFrame implements ActionListener,Runnable
{
int numberfirst,numbersecond,resualt;
String operating;
int count=0,numbercount=1;
long time;
boolean isanswered=false;
JLabel jlsettime=new JLabel(“a”);
JLabel jlsetresualt=new JLabel(“a”);
JLabel rightface=new JLabel( //new ImageIcon(“2.jpg”)//這個可以自己添加一個微笑的圖片,小一點
),wrongface=new JLabel( //new ImageIcon(“3.jpg”)//這個可以自己添加一個與微笑相反的圖片,小一點
),advice=new JLabel();
JButton jbok=new JButton(“ok”),next=new JButton(“next”);
JTextField jtanswer=new JTextField(4);
void createnumberandoperation()
{
int operat=(int)(Math.random()*4+1);//這裡定義1為加法,2為減法,3為乘法,4為除法
switch(operat)
{
case 1:
operating=”+”;
numberfirst=(int)(Math.random()*100);
numbersecond=(int)(Math.random()*(100-numberfirst));
resualt=numberfirst+numbersecond;
break;
case 2:
operating=”-“;
numberfirst=(int)(Math.random()*100);
if(numberfirst==0)createnumberandoperation();
numbersecond=(int)(Math.random()*numberfirst);
resualt=numberfirst-numbersecond;
break;
case 3:
operating=”*”;
numberfirst=(int)(Math.random()*100);
numbersecond=(int)(Math.random()*(100/numberfirst));//have a problem
resualt=numberfirst*numbersecond;
break;
case 4:
operating=”/”;
numberfirst=(int)(Math.random()*100);
numbersecond=(int)(Math.random()*100);
if(numbersecond==0)createnumberandoperation();//the number which is numbersecond cound not be 0;
resualt=numberfirst/numbersecond;//have a problem,
break;
}
}
public Caculater()
{
Gridbaglayoutadd gra=new Gridbaglayoutadd();
Container con=this.getContentPane();
GridBagLayout grid=new GridBagLayout();
con.setLayout(grid);
gra.setxandy(6, 6, grid, con);
int a5=0,a6=0,a7=0,a8=0,a9=0,a10=0,a11=0,a12=0;
gra.addobject(5, 2, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, jlsettime);
gra.addobject(2, 3, 3, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, jlsetresualt);
gra.addobject(2, 4, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, rightface);
gra.addobject(3, 4, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, wrongface);
gra.addobject(4, 4, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, advice);
gra.addobject(2, 5, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, jtanswer);
gra.addobject(3, 5, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, jbok);
gra.addobject(4, 5, 1, 1, a5, a6, a7, a8, a9, a10, a11, a12, grid, con, next);
jbok.addActionListener(this);
next.addActionListener(this);next.setMnemonic(‘n’);
jtanswer.addActionListener(this);
createnumberandoperation();
jlsetresualt.setText(numberfirst+operating+numbersecond+”=”);
setTitle(“一百以內加減乘除運算”);
setBounds(400,200,400,300);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{(e.getWindow()).dispose();
System.exit(0);
}});
}
public static void main(String s[])
{
JWindow jw=new JWindow();
jw.add(new JLabel(new ImageIcon(“1.jpg”)));//you can make a picture that welcome the guset
jw.setBounds(400, 200, 400, 400);
jw.setVisible(true);
try {
Thread.sleep(2000);
} catch (InterruptedException e) { }
jw.dispose();
Caculater ca=new Caculater();
ca.start();
}
public void actionPerformed(ActionEvent e)
{
if((e.getSource()==jtanswer||e.getSource()==jbok)!jtanswer.getText().equals(“”))
{if(!isanswered)
{
if(jtanswer.getText().equals(resualt+””))
{
advice.setText(“right”);
count++;
rightface.setVisible(true);
}
else
{advice.setText(“wrong”);
wrongface.setVisible(true);
}
jbok.setVisible(false);
isanswered=true;
if(numbercount==10)
{jtanswer.setVisible(false);
numbercount++;
jlsetresualt.setText(“您共答對了”+count+”道題,得分”+count*10+”分。”);
}
}
else
isanswered=false;
}
if(e.getSource()==next)
{
wrongface.setVisible(false);
rightface.setVisible(false);
isanswered=false;
jbok.setVisible(true);
numbercount++;
createnumberandoperation();
jlsetresualt.setText(numberfirst+operating+numbersecond+”=”);
jtanswer.setText(“”);
advice.setText(“”);
if(numbercount==10)
{
next.setVisible(false);
}
}
}
public void start()
{
run();
}
public void run() {
long time1,time2;
time1=System.currentTimeMillis();
for(;numbercount!=11;)
{
time2=System.currentTimeMillis()-time1;
jlsettime.setText(time2/1000+”秒”);
if(time2300*1000)
break;
}
numbercount=10;
jtanswer.setVisible(false);
jbok.setVisible(false);
next.setVisible(false);
jlsetresualt.setText(“您共答對了”+count+”道題,得分”+count*10+”分。”);
}
}
java隨機函數可以隨機指定的幾位數嗎?如果有,具體怎麼用?
將這三個數字放在數組中,然後隨機生成下標,就可以選擇了,java中有Random類,可以幫助你生成隨機數,你可以到f1208社區看看,裡面有很多數組的例子
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/254600.html