How do I make my Android app generate a random number?

23,102

Solution 1

How about Random.nextInt()?

Usage:

final Random rand = new Random();
int diceRoll = rand.nextInt(6) + 1; // uniformly distributed int from 1 to 6

In terms of updating an image, have the 6 different images you want to display (all the same size) and use a switch to select between different myImageView.setImageResource(R.drawable.dice_n).

Solution 2

Use a Random object with a long seed (System.currentTimeMillis() is a good one). Then call the nextInt(int n) method from your object and pass in the die size. (Remember that the range of nextInt(int n) starts at 0, so you'll want to pass in the die size, then add 1 to the resulting roll).

The reason for the long seed is to improve the (pseudo)randomness of the number distribution.

You should probably declare the die size as a constant, if you haven't already. Javadoc on Random here.

Share:
23,102
lonesarah
Author by

lonesarah

Updated on August 09, 2020

Comments

  • lonesarah
    lonesarah almost 4 years

    I'm new to Java and making android applications. How do you make a Java program that rolls a number of dice based on what the user entered?

    The Java program I created only rolls one dice.

    How do you get Java to roll randomly from one to six?

    How do you get Java to make random numbers based on the number of times the user wants?

    Lastly, how do you get Java to draw a image based on the number the user entered?

    Here what my application looks like.

    Valid XHTML .

    Here's my code

    package com.warhammerdicerrolleralpha;
    
    import java.util.Random;
    
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class myMain extends Activity 
    {
        /** Called when the activity is first created. 
         * @return */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
    
         final Random myRandom = new Random(6);
    
    
    
        Button buttonGenerate = (Button)findViewById(R.id.button1);
        final TextView textGenerateNumber = (TextView)findViewById(R.id.text4);
    
    
    
    
        buttonGenerate.setOnClickListener(new OnClickListener()
        {
    
               @Override
               public void onClick(View v) 
               {
                // TODO Auto-generated method stub
                textGenerateNumber.setText(String.valueOf(myRandom.nextInt()));
               }});
    
        }
    
    }
    

    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:padding="5dip" android:background="@drawable/warhammerdicerollalpha"
        android:layout_height="fill_parent">
    
    
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/hello" />
        <EditText android:layout_height="wrap_content"
            android:layout_width="match_parent" android:id="@+id/enternumberofdice"></EditText>
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/generatenumber" />
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="LOL" android:id="@+id/text4"></TextView>
    
    </LinearLayout>
    
  • lonesarah
    lonesarah about 13 years
    I get Cannot refer to a non-final variable myRandom inside an inner class defined in a different method
  • Maaalte
    Maaalte about 13 years
    nextInt(5) generates random numbers from 0 (included) to 5 (excluded). So you'll have to use nextInt(6)+1
  • fredley
    fredley about 13 years
    Thanks Maaalte, I've fixed it now.
  • fredley
    fredley about 13 years
    @lonesarah then declare it as final - add the final keyword before Random, as it is in your code already.
  • lonesarah
    lonesarah about 13 years
    Okay now eclipse wants to remove diceRoll because The local variable diceRoll is never read.
  • lonesarah
    lonesarah about 13 years
    Okay I fix it. Thanks fredley