开发者

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

开发者 https://www.devze.com 2023-02-16 01:48 出处:网络
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?

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.

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

.

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:lay开发者_JS百科out_height="wrap_content" android:text="LOL" android:id="@+id/text4"></TextView>

</LinearLayout>


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消