-1

I am making an app in Android studio that creates a series of random numbers and then uses them to make an equation, that the user answers.

The problem I have is that after they have awnsered correctly and press a button to generate a new equation, the program uses the same numbers it randomized the first time.

Things I have tried: putting the array outside the method, under class.

Using both Math.random and Random rng = new Random().

Make sure that the method is started several times, using System.out.println.

I have also tried the code in Eclipse, where it works like it should.

The randomiser in my code:

public void SkapaUppgift() {

    System.out.println(test);
    test=test+1;

    for (int i = 0; i < tal.length; i++) {
        Random rng= new Random();
        tal[i] = (int) (Math.random() * ((200 - 0) + 1) + 0);
        //tal[i] = rng.nextInt(200);
    }
}

edit: to clarify. I have used both Random rng AND math.random, and both have been specified to a range between 0 and 200. The problem is that every single time i call the method, the same random number gets pulled. For example: I start the app and get the number 20, 25, 200 and 3. The next time I call the method, I get 20, 25, 200 and 3. The third time I get 20, 25, 200 and 3. If I close the app and start it again, I get 4 different numbers. I want to get different numbers each time I call the method, not just when I restart the app.

Erik
  • 11
  • 1
  • If you want to generate a random integer within a range, look at this: http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java – Tunaki Dec 04 '15 at 19:02
  • I know how to make a random number, its just that I want to call the method more than one time, and get different numbers each time – Erik Dec 04 '15 at 19:03
  • 3
    Based on your comments in @Erick's answer, you found that the problem was not actually related to the RNG, but with how you were storing the numbers in an unrelated class. If this is the case, perhaps this question should be closed, since the error was caused by "a problem that can no longer be reproduced or a simple typographical error". – Mage Xy Dec 04 '15 at 19:54
  • The problem is solved so I have no problem with that – Erik Dec 04 '15 at 20:25

2 Answers2

2

You're creating a new Random instance, but not using it. Try using rng.nextInt() instead of Math.random().

The reason why this is not working is because Math.random() returns a random double between 0 (inclusive) and 1 (exclusive), but it's being downcast to an int, and, thus, being truncated, leaving you with only 1 possible outcome.

EDIT: In that case, you will need to include more code. Where is this being called from? Where are the results being printed out? I have a feeling that this method is working properly and you're actually just printing the same array each time.

Remember that objects in Java are passed by value, not by reference.

Andy Senn
  • 649
  • 5
  • 13
  • I have used both math.random and specified the range, as well as a Random instance which ive also specified to a range. The 200 part is the range, (Math.random() * ((200 - 0) + 1) + 0); returns a random int between 0 and 200 – Erik Dec 04 '15 at 19:09
  • It would seem to be something like that, im testing to see exactly what happens at every stage – Erik Dec 04 '15 at 19:27
0

You should get the results you want from rng.nextInt(). You just have to make sure you only make it one time and use the same instance forever after. In other words, make it a Singleton.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • The problem seems to be with how I saved my numbers – Erik Dec 04 '15 at 19:42
  • How did you save your numbers? – Erick G. Hagstrom Dec 04 '15 at 19:43
  • Saving them on the screen, in extremly small text, and then reading that text. However that text does not get replaced by the new numbers that are in fact being randomized – Erik Dec 04 '15 at 19:47
  • LOL well then, congratulations. You found and fixed your own problem. Best wishes in the future, my friend, and welcome to Stack Overflow! – Erick G. Hagstrom Dec 04 '15 at 19:50
  • So the new numbers are in fact being randomized, but not "properly"? I'm not following, sorry. – Erick G. Hagstrom Dec 04 '15 at 19:52
  • TL;DR I turned of the part that ran the calculations and saved the asignment. What I had done right was the randomisation. I made it print the numbers each time and they were randomising properly. The problem was that the calculation part, which included the part that saved the numbers was set so that only an awnser between 1 and 200 would be accepted. That was set in a while loop, that was turned off after an accepted anwser. That also turned off the calculation part, which made it so that it used the same numbers, as no new ones were set. – Erik Dec 04 '15 at 20:01