-5

I've made a program that generates a random number but every time it gives back 0.0

Program:

import java.util.*;

public class RandomNumber {

    public static void main(String args[]){

        double QuantityColors = 5;
        double Mastermind = 0;

        Random(QuantityColors, Mastermind);
        System.out.println(Mastermind);
    }

    public static double Random(double QuantityColors, double Mastermind){

        Mastermind = Math.random();
        Mastermind = Mastermind * QuantityColors;
        Mastermind = (int) Mastermind;

        return Mastermind ;
    }
}

I've been searching where the problem is, but the problem is in the return.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • 1
    Java is pass by value for primitives, if you pass a primitive like a double to a method it will not modify it. – Benjamin Gruenbaum May 30 '15 at 21:06
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?answertab=votes#tab-top see – Benjamin Gruenbaum May 30 '15 at 21:07
  • 1
    Your code is not within naming conventions. Local variables, fields, and parameter names should be `camelCase` and not `PascalCase`. – nanofarad May 30 '15 at 21:10
  • your call to Random is not assigning the result to any variable in main(). The MasterMind variable in Random is not the same one as in main(). – swingMan May 30 '15 at 21:37

3 Answers3

4

a) you are doing nothing with the result of "Random".

b) you can not modify Java argument. See change a functions argument's values?

Community
  • 1
  • 1
pasaba por aqui
  • 3,446
  • 16
  • 40
3

First of all, you can use a builtin function to generate a next integer with a certain upper bound: Random.nextInt(int). For instance:

Random rand = new Random();
int masterMind = rand.nextInt(QuantityColors);

Instead of writing a Random method yourself.

It is nearly always better to use builtins since these have been tested extensively, are implemented to be rather fast, etc.

Next you seem to assume that Java uses pass-by-reference. If you perform the following call:

Random(QuantityColors, Mastermind);

Java will make a copy of the value of MasterMind. Setting the parameter in a method has no use. The only way to set a value - not encapsulated in an object - is by returning value. So:

MasterMind = Random(QuantityColors, Mastermind);

To make a long story short: the method does not return 0, you simply don't do anything useful with it.

A better solution would thus be to drop the Random method and use:

import java.util.*;

public class RandomNumber {

    public static void main(String args[]){

        int quantityColors = 5;
        Random rand = new Random();

        int mastermind = rand.nextInt(QuantityColors);
        System.out.println(mastermind);
    }
}

Further remarks

In your random method:

public static double Random(double QuantityColors, double Mastermind){

the MasterMind parameter is rather useless since you immediately set it with another value, so you better remove it and use a local variable instead.

Furthermore Java standards say that the name of classes, interfaces, etc. start with an uppercase; the names of methods and variables with lowercase.

Finally it is unclear why you use doubles since all the values you calculate are clearly integral.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

It looks like your code would work if you wrote

 Mastermind = Random(QuantityColors, Mastermind);

...because Java is pass by value, so calling a function will not change the variable you passed in.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413