0

我希望你能帮助我处理这段代码。我可以生成随机数,但不能精确计数。

问题是,单击按钮后如何生成 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
4

7 回答 7

5

尝试在此函数中使用 JAVA 的数学和随机类来生成具有“n”位数字的随机数。例如 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
于 2014-07-18T10:23:07.333 回答
1

您可以使用倒数计时器。http://developer.android.com/reference/android/os/CountDownTimer.html

这是一个例子:http ://sree.cc/google/android/android-timer-example

于 2013-03-11T04:58:18.493 回答
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();
    }

  }
于 2013-03-11T05:21:02.293 回答
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());
}

要定期执行,请查看这篇文章: 如何在 Android 中运行 Runnable 线程?

于 2013-03-11T04:59:53.263 回答
0

您可以使用 Java Random类:

final int MAX_VALUE = 100;
Random rand = new Random();
int randValue1 = rand.nextInt(MAX_VALUE);
int randValue2 = rand.nextInt(MAX_VALUE);
于 2013-03-11T05:04:11.343 回答
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 ;
}
于 2013-03-11T05:04:35.753 回答
0

生成一个低于 1000 万的数字,然后将其格式化为 7 位数字:

public String gen7DigitNumber()
{
    Random rng = new Random();
    int val = rng.nextInt(10000000);
    return String.format("%07d", val);
}
于 2013-05-08T09:43:04.417 回答