[Java]Unique Random Variables [Solved]

Joined
Jul 5, 2010
Messages
46
Reaction score
15
Okay, I'm not very experienced in Java, so I needed a way to script an event for my Maplestory server..

The only thing I really need help is with a function (randomizeRopes();)
to do this:

Assign a number from 1-14 for 14 different variables, but each variable must have a different value.
So , when I do randomizeRopes, what would happen is

var1 = 13
var2 = 11
var3 = 4
var4 = 3
var5 = 9
...You get the point.
Thing is, the only thing I know is Math.floor(Math.random * 13 + 1) thing.

So..?
Any help would be appreciated.
 
Last edited:
Re: [Help]Unique Random Variables

One way is adding your numbers 1-14 into a list (ArrayList is fine) and calling its shuffle() method, then extract them in order from the beginning.

edit: Sorry, shuffle() isn't a member of the list, it's a static function in Collections. So you'd call Collections.shuffle(myList).
 
Last edited:
Re: [Help]Unique Random Variables

No idea how to do that, so I'll just look up what you said on google, I should be able to figure it out. Thanks! C:
Oh, didn't realize you needed it more detailed. You could use this for any other data type, too.
Code:
// The list to hold the numbers
ArrayList<Integer> numbers = new ArrayList<Integer>();

// Put all the values you want into the list
for (int i = 1; i <= 14; i++)
  numbers.add(i);

// ... and shuffle
Collections.shuffle(numbers);
Then, instead of having the variables var1, var2, var3, and so on, you'll have numbers.get(0), numbers.get(1), numbers.get(2), and so on. Note, that indexing is from 0 to 13.

You could, of course, perform the same manually, too. Here's the description of what shuffle really does: Collections (Java 2 Platform SE 5.0)
 
Last edited:
Re: [Help]Unique Random Variables

One way is adding your numbers 1-14 into a list (ArrayList is fine) and calling its shuffle() method, then extract them in order from the beginning.

edit: Sorry, shuffle() isn't a member of the list, it's a static function in Collections. So you'd call Collections.shuffle(myList).

Knuth shuffle = win, and brilliant.
 
Re: [Help]Unique Random Variables

Aha, i totally understand this now - figured out how the for statement things work too. If I'm right, I'd also have to
PHP:
import.java.util.Collections
?
Thanks. c:
Oh, so you are totally new to programming. Cool that you got it figured out. :thumbup:

Yeah you have to import something, probably that one. I haven't kept track where everything is located.
 
Back