-1

Inside the brackets of

public class MainActivity extends AppCompatActivity

I have (among other things):

public float numAlea() {
        return r.nextFloat();
    }

Inside OnCreate I have:

r = new Random();

Then, I have a private void method that starts with

random0to1 = numAlea();

and uses 'random0to1' as if it was a random number, but it seems that this random0to1 is always a number somewhere between 2/3 and 1, because between three buttons that should "pop" randomly each time this private void method is called only the third button is "popped". What is wrong with this code?

Thank you very much for your time!

I believe this question is different from the one suggested by Stack Overflow, because I am not using seeds (and I guess I don't need to, because I once had a similar problem when building another app and managed to fix it - unfortunately, using the same structure of that app regarding random numbers didn't fix the issue I'm presenting here).

UPDATE:

MainActivity.java

package com.example.android.whack_a_lock_022;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    int cl = 0;
    int nl = 0;
    int sl = 0;
    int whacks = 0;
    int maxwhacks = 0;
    Random r;
    float random0to1;
    private CountDownTimer countDownTimer;
    private Button start;
    private Button cancel;
    private TextView time;

    public float numAlea() {
        return r.nextFloat();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        r = new Random();

        Button clButton = findViewById(R.id.cl);
        Button nlButton = findViewById(R.id.nl);
        Button slButton = findViewById(R.id.sl);

        View.OnClickListener btnClickListener = new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                switch(v.getId()) {
                    case R.id.startButton :
                        start();
                        break;
                    case R.id.cancelButton :
                        cancel();
                        break;
                }
            }
        };

        clButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cl == 1) {
                    cl = 0;
                    TextView CapsLock = findViewById(R.id.cl);
                    CapsLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        nlButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nl == 1) {
                    nl = 0;
                    TextView NumLock = findViewById(R.id.nl);
                    NumLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        slButton.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sl == 1) {
                    sl = 0;
                    TextView ScrollLock = findViewById(R.id.sl);
                    ScrollLock.setText("0");
                    whacks++;
                    TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
                    NumberOfWhacks.setText(String.valueOf(whacks));
                    if (whacks > maxwhacks) {
                        maxwhacks = whacks;
                        TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
                        NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
                    }

                    pop();
                }
            }
        });

        // Capture our button from layout
        start = (Button) findViewById(R.id.startButton);
        start.setOnClickListener(btnClickListener);
        cancel = (Button) findViewById(R.id.cancelButton);
        cancel.setOnClickListener(btnClickListener);
        time = (TextView) findViewById(R.id.time);

        // Register the onClick listener with the implementation above

    }

    private void start () {

        time.setText("60");

        countDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time.setText("" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish(){
                time.setText("Done !");
            }
        };
        countDownTimer.start();

        pop();

    }

    private void pop(){
        // Escolhe e "POPa" o botão

        // Gera número aleatório

        random0to1 = numAlea();

        if (random0to1 <= (1 / 3)) {

            cl = 1;
            Button clButton = (Button) findViewById(R.id.cl);
            clButton.setText("1");

        } else if (random0to1 <= 2 / 3) {
            nl = 1;
            Button nlButton = (Button) findViewById(R.id.nl);
            nlButton.setText("1");
        } else if (random0to1 <= 1) {
            sl = 1;
            Button slButton = (Button) findViewById(R.id.sl);
            slButton.setText("1");
        }
    }

    private void cancel() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }

        //Põe botões a zeros

        cl = 0;
        Button clButton = (Button) findViewById(R.id.cl);
        clButton.setText("0");

        nl = 0;
        Button nlButton = (Button) findViewById(R.id.nl);
        nlButton.setText("0");

        sl = 0;
        Button slButton = (Button) findViewById(R.id.sl);
        slButton.setText("0");

        whacks = 0;
        TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
        NumberOfWhacks.setText("0");

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/whacksView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="WHACKS"
            android:textSize="24sp"
            android:textAlignment="center"/>
        <TextView
            android:id="@+id/maxWhacksView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="MAX WHACKS"
            android:layout_weight="1"
            android:textSize="24sp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="60"
        android:textSize="24sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/numberOfWhacksView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0"
            android:textAlignment="center"
            android:textSize="24sp"/>
        <TextView
            android:id="@+id/numberOfMaxWhacksView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="0"
            android:layout_weight="1"
            android:textSize="24sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/cl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/nl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/sl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1"/>
    </LinearLayout>

    <Button
        android:id="@+id/startButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Start" />

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />

</LinearLayout>
  • 1
    You could try instantiating Random with the current system time: `new Random(System.currentTimeMillis());`. Or just make a new Random object (pass the current system time) every time you call it. – TheWanderer Sep 06 '18 at 15:09
  • possible duplicate: https://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed – sebasira Sep 06 '18 at 15:09
  • Possible duplicate of [Java random always returns the same number when I set the seed?](https://stackoverflow.com/questions/5533191/java-random-always-returns-the-same-number-when-i-set-the-seed) – sebasira Sep 06 '18 at 15:10

1 Answers1

0

I think an error is in this part of the code - see my comments:

 private void pop(){
        random0to1 = numAlea();

        if (random0to1 <= (1 / 3)) {             // 1/3 == 0 , try to use 1F / 3

            cl = 1;
            Button clButton = (Button) findViewById(R.id.cl);
            clButton.setText("1");

        } else if (random0to1 <= 2 / 3) {       // As same as in a previous branch
            nl = 1;
            Button nlButton = (Button) findViewById(R.id.nl);
            nlButton.setText("1");
        } else if (random0to1 <= 1) {
            sl = 1;
            Button slButton = (Button) findViewById(R.id.sl);
            slButton.setText("1");
        }
    }
Alex Shevelev
  • 681
  • 7
  • 14