0

I have it set up so If a 0 is returned, than an image of a dice with a 1 on it is set to the place of a buttons image. It does this with different values and images too. 0 = Dice 1, 1 = Dice 2, 2 = Dice 3, 3 = Dice 4, 4 = Dice 5. The problem is that all numbers returned are always 0, I know this because the buttons all have a one image on them. I have looked at countless tutorials and have no idea what is going on! Please help me internet friends!

Here is my code:

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;
}
Drew Stauft
  • 107
  • 11

4 Answers4

0

First, you should initialize Random with a time seed:

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

And then, you can get your int value between 0 and max with

int i = rand.nextInt(max);
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Shouldn't need to do the call to currentTimeMillis, Javadoc says `public Random(long seed) Creates a new random number generator using a single long seed. The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).` – pjs Apr 29 '13 at 22:36
0

you can use SimonSays answer or if you want to generate numbers between a specific range you can use this

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

this will generate random number between 1 to 11

Elior
  • 3,178
  • 6
  • 37
  • 67
0

Example random generator:

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) can generate -150, -175 (numbers from -200 to -100), but can't generate 150. First parametr must smaller than second.

barwnikk
  • 950
  • 8
  • 14
0

Your logic is correct. You can generate a Random number on that way. I implemented here exactly like you, take a look(You can find the full example here: , click o random example on the main github full example:

Probably your mistake is at the moment to get the images if they depends of the random number

Here is my activity code:

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

And here my layout:

<?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>
Bruno Mateus
  • 1,727
  • 18
  • 25