简体   繁体   中英

generate random numbers in methods

I did the code, make a random number between 1 and 100 as task A), then how I understood if the first value if more than 50 generate second random number between 1 and 50 as said ( how I think) task B)

PLEASE can any one explain what the task C and D is, not understand how to do it at all ..(((pls help with advice or explanation task C and D.

Thanks....

TASK:

Write a program that generates random numbers:

a) +Write a method that returns a random integer in the range of 1 to 100.

b) Then add another method that takes a parameter specifying the top number – ie if you pass it 50 it returns a random number between 1 and 50. Test your random method and make sure it works.

c) Write another method so that you pass it two values – the top and bottom of the range you want the highest value from – ie if you pass it 10 and 20 it returns a random number between 10 and 20.

d) Then write another method so that if repeatedly called it doesn't return two numbers the same? So once a random number has been generated and returned, the method doesn't return that number again. To do this you will have to store every number generated.

import java.util.Random;

public class ranGen {

    public Integer random (Integer integer){
        Random rand = new Random();;
        int min=0, max=100;
        int randomNum = rand.nextInt(max - min + 1) + min;

        System.out.println(randomNum);
        return randomNum;
        }


    public void random50 (Integer integer){


        Random rand = new Random();;
        int min=0, max=50;
        int randomNum = rand.nextInt(max - min +1) + min;

        System.out.println(randomNum);
        }


    public static void main(String[] args) {
        ranGen process = new ranGen();

        if(process.random(null) > 50){
            process.random50(null);
        }

    }

}

c:

public Integer randomC(Integer min, Integer max) {
    //int min = 0, max = 0;
    ...
}

d:

public class ranGen {
    ArrayList<Integer> nums;
    ...

    public Integer randomD() {
        ...
        randomNum = rand.nextInt(max - min + 1) + min;
        while(nums.Exists(randomNum)) {
            randomNum = rand.nextInt(max - min + 1) + min;
        }
        nums.Add(randomNum);
        return randomNum;
    }
}

Note: arraylist and methods shown here for info purpose.

The goal of the homework, it seems, is to make you learn what method parameters are. The first step is to write a method that returns a random integer in the range of 1 to 100. So this method doesn't have any parameter. Its signature should be

public int generateRandomNumberBetween0And100()

The second step is to write a method which takes a parameter specifying the top number. Its signature should thus be:

public int generateRandomNumberBetween0And(int topValue)

The third step is to write a method taking two values as parameter: the bottom and the top value. Its signature should thus be:

public int generateRandomNumberBetween(int bottomValue, int topValue)

The last step is a bit more tricky. It asks you to remember the values a method has already generated, and to avoid generating them again. There is no parameter. Its signature should be

public int generateUniqueRandomNumber()

and its implementation should basically do the following:

1. generate random number
2. if random number is in the set of already generated numbers, go to 1
3. store the generated number in the set of already generated numbers
4. return the generated number.

Of course, the set of already generated numbers is empty at the beginning.

Note that you should use int rather than Integer . There's no reason to accept null as argument, and the methods should never return null. So the primitive int type should be preferred.

Also note that the methods should not print the generated number, but return it. So they should have int as their return type, and not void .

And classes, by convention, start with an upper-case letter and don't abbriviate words. So your class should be named RandomGenerator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM