5 Replies - 997 Views - Last Post: 12 January 2019 - 10:46 AM Rate Topic: -----

#1 thewillisonq   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 19

League of Legends random selection

Posted 12 January 2019 - 08:27 AM

Hey guys I'm new to this whole java thing but I'm learning. I'm trying to create a program that will randomly select a name in a whole selection of names. I've checked the java api a couple times and didn't find anything. If your guys have any suggestions I'd be happy to hear them. Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: League of Legends random selection

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 750
  • View blog
  • Posts: 5,633
  • Joined: 25-December 13

Re: League of Legends random selection

Posted 12 January 2019 - 08:32 AM

See the Random class for ways to generate random numbers that could be used as an index into an array.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12506
  • View blog
  • Posts: 45,641
  • Joined: 27-December 08

Re: League of Legends random selection

Posted 12 January 2019 - 09:12 AM

See this tutorial on arrays. Once you have your array, you can use the java.util.Random class nextInt() method to get a random index.

String[] names = { /* populate with names */ };
Random rand = new Random();

//returns random int in the interval [0, rand.length) 
int index = rand.nextInt(names.length);

//access the name at the random index
String randomName = names[index]; 


This post has been edited by macosxnerd101: 12 January 2019 - 09:27 AM
Reason for edit:: Fixed typo

Was This Post Helpful? 1
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 750
  • View blog
  • Posts: 5,633
  • Joined: 25-December 13

Re: League of Legends random selection

Posted 12 January 2019 - 09:21 AM

Should code use: names.length
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12506
  • View blog
  • Posts: 45,641
  • Joined: 27-December 08

Re: League of Legends random selection

Posted 12 January 2019 - 09:27 AM

Thanks for catching that typo, NormR! I just fixed it. :)
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3611
  • View blog
  • Posts: 16,552
  • Joined: 20-September 08

Re: League of Legends random selection

Posted 12 January 2019 - 10:46 AM

The approach depends on your definition of 'select'. If, as is often the case, 'select' means choose and remove a random object from a collection, then you should be looking at using a Collection class, such as ArrayList to hold that collection and calling remove() on it
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1