Random | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

3rd May 2017, 5:57 AM
Royce
4 Answers
+ 10
yes by chosing the index of the myNames array randomly
3rd May 2017, 11:38 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 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); } } }
3rd May 2017, 6:25 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
0
rn.nextInt (x); for every name
3rd May 2017, 6:23 AM
Daniel S
Daniel S - avatar
0
can i also print the names in random order?
3rd May 2017, 11:03 PM
Royce