Click here to Skip to main content
15,671,117 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the method, but it currently only prints a random number.

What I have tried:

<pre lang="Java"><pre><pre lang="Java">/**
     * Print multiple random intergers values using System.out.println.
     * @param howMany How many random values to print.
    public void printMutliRandom(int howMany){
        Random rand = new Random();
Updated 24-Nov-22 10:08am

1 solution

If you want to print a random number N times, you need to loop round N times, printing a single random value each time ...

And don't declare the Random instance inside a method: it is initialised from the system clock when it is created, so with modern processors it's quite likely to generate the same sequence repeatedly if you call the method several times. Declare and initialize it at class level instead.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900