我希望你能帮我解决这个问题。我可以生成随机数但不是精确计数。
问题是,一旦点击按钮,如何生成7个随机数字?
请参阅下面的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Random myRandom = new Random();
Button buttonGenerate = (Button)findViewById(R.id.generateme);
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
}
});
}
}
这是我的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"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate Random number"
android:id="@+id/generateme"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/generatenumber"
/>
</LinearLayout
答案 0 :(得分:5)
尝试使用JAVA的Math和Random类,就像在此函数中一样,使用&#34; n&#34;生成随机数。数字。例如7位数的随机数。
private int nDigitRandomNo(int digits){
int max = (int) Math.pow(10,(digits)) - 1; //for digits =7, max will be 9999999
int min = (int) Math.pow(10, digits-1); //for digits = 7, min will be 1000000
int range = max-min; //This is 8999999
Random r = new Random();
int x = r.nextInt(range);// This will generate random integers in range 0 - 8999999
int nDigitRandomNo = x+min; //Our random rumber will be any random number x + min
return nDigitRandomNo;
}
int digits = 7;
int n = nDigitRandomNo(digits);
//n will be a random number between 1000000 - 9999999
答案 1 :(得分:1)
答案 2 :(得分:0)
int[] rand = new int[7];
int ceiling = 100; // for random numbers from 0 to 99
for (int i=0; i < rand.length; i++) {
rand[i] = (int)Math.floor(ceiling*Math.rand());
}
要定期执行,请查看此帖子: How to run a Runnable thread in Android?
答案 3 :(得分:0)
您可以使用Java Random类:
final int MAX_VALUE = 100;
Random rand = new Random();
int randValue1 = rand.nextInt(MAX_VALUE);
int randValue2 = rand.nextInt(MAX_VALUE);
答案 4 :(得分:0)
/** Returning the Random Numbers*/
public String random () throws NoSuchAlgorithmException {
Random ranGen = new SecureRandom();
byte[] randomno= new byte[8];
ranGen.nextBytes(randomno);
String str = new String(randomno, "UTF8");
return str ;
}
答案 5 :(得分:0)
修改您的代码,如下所示:
public class MainActivity extends Activity {
public static int[] randomNumbers=new int[7];
public static byte[] sevenDigits=new byte[7];
private static boolean status=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Random myRandom = new Random();
genRandomNumbers();
while(status);
for (int i : randomNumbers) {
System.out.println(i);
}
System.out.println("seven digit random number : "+new String(sevenDigits));
Button buttonGenerate = (Button)findViewById(R.id.generateme);
final TextView textGenerateNumber = (TextView)findViewById(R.id.generatenumber);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
}
});
}
public void genRandomNumbers() {
new Thread(new Runnable() {
@Override
public void run() {
Random random=new Random();
while(true) {
for(int i=0; i<7 ;i++) {
randomNumbers[i]=random.nextInt(10);
sevenDigits[i]=random.nextInt(10);
}
status=false;
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
答案 6 :(得分:0)
生成一个低于1000万的数字,然后将其格式化为7位数:
public String gen7DigitNumber()
{
Random rng = new Random();
int val = rng.nextInt(10000000);
return String.format("%07d", val);
}