0
Random
once it selects a random number it sticks it with every name, I want a different random number for each name https://code.sololearn.com/c2ICc8f8UPII/?ref=app
4 Answers
+ 10
yes by chosing the index of the myNames array randomly
+ 9
you must put the r = rn.nextInt(3);
in side the for loop not out side in oder to get new random number each time.
otherwise, you will get the same result
import java.util.Random;
public class Program {
public static void main(String[] args) {
String[ ] myNames = { "Fred", "Tom", "George", "Tina"};
Random rn = new Random();
int r ;
for(int i= 0; i<=3; i++){
r = rn.nextInt(3);
System.out.println(myNames[i] + " is " + r);
}
}
}
0
rn.nextInt (x); for every name
0
can i also print the names in random order?