2010年5月10日星期一

Android的隨機數字產生器

Android提供java.util.Random類以產生不同類型,如int, long, double和float的偽隨機(pseudo-random)數.

Android的隨機產生器

它提供兩個公共構造函數, Random()和Random(long seed).
Random(): 以當前時間的毫秒位作為初始狀態, 建構一個隨機數字產生器.
Random(long seed): 以一個特定的種子數作為初始狀態, 建構一個隨機數字產生器.

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/milliseconds"
android:id="@+id/contructor" >
<RadioButton
android:text="Random()"
android:id="@+id/milliseconds" />
<RadioButton
android:text="Random(long seed)"
android:id="@+id/seed" />
</RadioGroup>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/typeint"
android:id="@+id/type" >
<RadioButton
android:text="int"
android:id="@+id/typeint" />
<RadioButton
android:text="long"
android:id="@+id/typelong" />
<RadioButton
android:text="double"
android:id="@+id/typedouble" />
<RadioButton
android:text="float"
android:id="@+id/typefloat" />
</RadioGroup>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/seeknumber"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random number"
android:id="@+id/generate"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/generatednumber"
/>
</LinearLayout>


AndroidRandom.java
package com.AndroidRandom;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class AndroidRandom extends Activity {

RadioButton rbRandomMilliseconds, rbRandomSeek;
RadioButton rbInt, rbLong, rbDouble, rbFloat;
EditText etSeekNumber;
Button buttonGenerate;
TextView textGeneratedNumber;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

rbRandomMilliseconds = (RadioButton)findViewById(R.id.milliseconds);
rbRandomSeek = (RadioButton)findViewById(R.id.seed);
rbInt = (RadioButton)findViewById(R.id.typeint);
rbLong = (RadioButton)findViewById(R.id.typelong);
rbDouble = (RadioButton)findViewById(R.id.typedouble);
rbFloat = (RadioButton)findViewById(R.id.typefloat);
etSeekNumber = (EditText)findViewById(R.id.seeknumber);
buttonGenerate = (Button)findViewById(R.id.generate);
textGeneratedNumber = (TextView)findViewById(R.id.generatednumber);

buttonGenerate.setOnClickListener(buttonGenerateOnClickListener);
}

private Button.OnClickListener buttonGenerateOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {

Random myRandom;

// TODO Auto-generated method stub
if (rbRandomMilliseconds.isChecked()){
myRandom = new Random();
}
else{
Long tSeek;
if (etSeekNumber.getText().toString().equals(""))
{
tSeek = 0L;
etSeekNumber.setText("0");
}
else
{
tSeek = Long.decode(etSeekNumber.getText().toString());
}
myRandom = new Random(tSeek);
}

if (rbInt.isChecked()){
textGeneratedNumber.setText(String.valueOf(myRandom.nextInt()));
}
else if (rbLong.isChecked()){
textGeneratedNumber.setText(String.valueOf(myRandom.nextLong()));
}
else if (rbDouble.isChecked()){
textGeneratedNumber.setText(String.valueOf(myRandom.nextDouble()));
}
else{
textGeneratedNumber.setText(String.valueOf(myRandom.nextFloat()));
}
}

};

}


相關文章: 關於pseudo-random



沒有留言:

發佈留言