为什么我的随机发生器总是返回0?

时间:2013-04-29 20:04:46

标签: android eclipse random numbers generator

我已将其设置为如果返回0,则将其上带有1的骰子的图像设置为按钮图像的位置。它也可以使用不同的值和图像。 0 =骰子1,1 =骰子2,2 =骰子3,3骰子4 =骰子5.问题是返回的所有数字总是0,我知道这是因为按钮都有一个图像。我看了无数的教程,不知道发生了什么!请帮助我的网友!

这是我的代码:

package com.example.yahtzee;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {

static int mButtonAmount = 7;
static int SidesOfDice = 6;
final static int NumberOfDice = 6;
int Rolls = 1;
boolean mCACV = true;
String dialog_title;
String dialog_message;
String positive_button;
String neutral_button;
String negitive_button;
int DiceNumber;
int placeHolder;
int max = NumberOfDice + 1;

Button[]mButtons = new Button[mButtonAmount];
Boolean[]mButtonBools = new Boolean[mButtonAmount];
Drawable[]mDiceImages = new Drawable[SidesOfDice];
Boolean[]mIsDiceHeld = new Boolean[NumberOfDice];
ImageButton[]mImageButtons = new ImageButton[NumberOfDice];
int[]DieNumber = new int[NumberOfDice];

private static final Random RANDOM = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButtons[0] = (Button) findViewById(R.id.start_roll);
    mButtons[0].setEnabled(true);
    mButtons[1] = (Button) findViewById(R.id.stop_roll);
    mButtons[1].setEnabled(false);
    mButtonBools[0] =  getResources().getBoolean(R.bool.is_start_roll_button_pressable);
    mButtonBools[1] = getResources().getBoolean(R.bool.is_stop_roll_button_pressable);

    mDiceImages[0] = getResources().getDrawable(R.drawable.diceside1);
    mDiceImages[1] = getResources().getDrawable(R.drawable.diceside2);
    mDiceImages[2] = getResources().getDrawable(R.drawable.diceside3);
    mDiceImages[3] = getResources().getDrawable(R.drawable.diceside4);
    mDiceImages[4] = getResources().getDrawable(R.drawable.diceside5);
    mDiceImages[5] = getResources().getDrawable(R.drawable.diceside6);

    mImageButtons[0] = (ImageButton) findViewById(R.id.die_1);
    mImageButtons[0].setClickable(false);
    mImageButtons[1] = (ImageButton) findViewById(R.id.die_2);
    mImageButtons[1].setClickable(false);
    mImageButtons[2] = (ImageButton) findViewById(R.id.die_3);
    mImageButtons[2].setClickable(false);
    mImageButtons[3] = (ImageButton) findViewById(R.id.die_4);
    mImageButtons[3].setClickable(false);
    mImageButtons[4] = (ImageButton) findViewById(R.id.die_5);
    mImageButtons[4].setClickable(false);

    mIsDiceHeld[0] = false;
    mIsDiceHeld[1] = false;
    mIsDiceHeld[2] = false;
    mIsDiceHeld[3] = false;
    mIsDiceHeld[4] = false;

    controlVariables();
}

public void controlVariables()
{
    rollDice();
            numberToImage();
}

public void checkAndChangeButtons()
{
    checkStartButton();
    checkStopButton();
}


public void rollDice()
{
    for(int i = 0; i < DieNumber.length; i++)
    {
    }

public void numberToImage()
{
    for(int i = 0; i < 5; i++)
    {
        mImageButtons[i].setBackground(mDiceImages[DieNumber[i]]);
    }
}

public int nextInt(int n)
{
    n = RANDOM.nextInt();
    return (n < 0 ? -n : n) % max;
}

4 个答案:

答案 0 :(得分:0)

首先,您应该使用时间种子初始化Random:

Random rand = new Random(System.currentTimeMillis());

然后,您可以使用

获取介于0和最大值之间的int值
int i = rand.nextInt(max);

答案 1 :(得分:0)

您可以使用SimonSays答案,或者如果您想在特定范围之间生成数字,可以使用此

 Random generator = new Random();
 int i = generator.nextInt(10) + 1;

这将产生1到11之间的随机数

答案 2 :(得分:0)

随机生成器示例:

long RandomGenerator(long from,long to) throws InvalidParameterSpecException{
    if(to<from){throw new InvalidParameterSpecException("Parametr 'from' must bigger than 'to'");}
    return System.currentTimeMillis()%(to-from)+from;
}

RandomGenerator(-200,-100)可以生成-150,-175(数字从-200到-100),但不能生成150.第一个参数必须小于秒。

答案 3 :(得分:0)

你的逻辑是正确的。您可以通过该方式生成随机数。 我在这里完全像你一样实现,看看(你可以在这里找到完整的例子:,点击主github full example上的随机示例:

如果他们的图像取决于随机数,可能你的错误就是获取图像

这是我的活动代码:

public class RandomNumberActivity extends SherlockActivity{

   private static final Random RANDOM = new Random();
   private int count;

   private TextView numbers;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.random_numbers);
        count = 0;
        numbers = (TextView) findViewById(R.id.numbers);

   }


   public void generate(View button)
   {        
        int i = RANDOM.nextInt();
        count++;
        i = (i < 0 ? -i : i) % 7;

        String current = numbers.getText().toString();
       numbers.setText(current.concat(count+"º: " + i+",\n"));      
   }
}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button android:id="@+id/generate"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/generate"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="generate"
    />

    <ScrollView 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_above="@id/generate">
        <TextView 
             android:id="@+id/numbers"
             android:layout_height="wrap_content"
             android:layout_width="match_parent"
             android:textColor="@android:color/black"/>
</ScrollView>


</RelativeLayout>