0

I would like you to help me on this code. I can generate random numbers but not in exact count.

Question is, how can I generate 7 random digits once button is click?

Please refer to my code below:

    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()));
                  }
       });
  }
  }

Here's is my XML file

     <?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
jun
  • 35
  • 2
  • 12
  • why can't you put the random number generation code in a `for`, which executes 7 times at a go? – Rahul Mar 11 '13 at 04:57

7 Answers7

5

Try this using Math and Random classes of JAVA as in this function to generate random numbers with "n" digits. For example random number with 7 digits.

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
LivePwndz
  • 600
  • 1
  • 8
  • 14
1

You can use a countdown timer. http://developer.android.com/reference/android/os/CountDownTimer.html

Here is an example: http://sree.cc/google/android/android-timer-example

nedaRM
  • 1,837
  • 1
  • 14
  • 28
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());
}

To execute at regular intervals check out this post: How to run a Runnable thread in Android?

Community
  • 1
  • 1
eski
  • 7,917
  • 1
  • 23
  • 34
  • You could use the countdown timer mentioned earlier, or a handler & runnable with postDelayed, I will add a link to that in my answer. – eski Mar 11 '13 at 05:10
0

You can use Java Random class:

final int MAX_VALUE = 100;
Random rand = new Random();
int randValue1 = rand.nextInt(MAX_VALUE);
int randValue2 = rand.nextInt(MAX_VALUE);
Hartok
  • 2,147
  • 20
  • 37
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 ;
}
Hartok
  • 2,147
  • 20
  • 37
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

Modify your code as follows :

    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();
    }

  }
Visruth
  • 3,430
  • 35
  • 48
  • nothing happen, program work but it doesn't generate 7 random numbers.. T_T – jun Mar 11 '13 at 06:55
  • @jun I have modified the code. `randomNumbers` contains 7 random numbers so you may use where ever you need. What it prints?? – Visruth Mar 11 '13 at 07:09
  • when button is click it generate random numbers but not 7 digit numbers.. when i generate again the output is 9 digit random numbers, 8 digit random numbers, 10 digit random numbers and so on. i just need it to generate 7 random digits number. – jun Mar 11 '13 at 07:28
0

Generate a number below 10 million, then format it as 7 digits:

public String gen7DigitNumber()
{
    Random rng = new Random();
    int val = rng.nextInt(10000000);
    return String.format("%07d", val);
}
nitegazer2003
  • 1,193
  • 5
  • 10