Picking some numbers randomly from a pool of integers :-)

Hello!

I want a code to select randomly let's say 3 numbers from a pool of numbers like (1,2,4,7,8,34) which have evenly distributed probabilities.

How this can be done?

I have found only how to pick a number randomly from a range of integers, but this is not my case, as I don't want the possibility of a number being selected more than once. And also I have a 'pool' of integers, not just a range of them :(

Thanks

[451 byte] By [miss_eleca] at [2007-11-27 10:03:30]
# 1
Look at the java.util.Collections.shuffle() method.
prometheuzza at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Put your "pool" into an array or arrylist and generate random numbers to use as an index from which to pull the number.
masijade.a at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 3
Yes, I thought of that but I don't want the same number to be picked more than once. Maybe I'll but some if statements to avoid that.
miss_eleca at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 4
As you pick numbers from the set, you could remove them. Or you could remember the set of numbers chosen so far, and repeat the selection when a chosen value was previously selected. None of this is hard or tricky to do.
BigDaddyLoveHandlesa at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 5
The shuffle thing sounds better...I have to create a List from the array of integers I have? And after the shuffling how do I 'take' the first elements of this List that I want?
miss_eleca at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 6
It sounds like you would benefit from taking the collections tutorial. http://java.sun.com/docs/books/tutorial/collections/index.html
BigDaddyLoveHandlesa at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 7

I have found in this site http://www.sics.se/humle/socialcomputing/download/javadoc/util/javadoc/se/sics/util/DataTypeUtils.html

a method shuffle(int[] array, Random random) which is exaclty what I look...but do you have any idea of how I can incorporate these in my library? I use JBuilder with 1.4 java version.

miss_eleca at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 8

What's wrong with using the shuffle method of java.util.Collections?

import java.util.*;

public class ShuffleExample {

public static void main(String... args) {

Integer[] array = {2, 3, 5, 7, 9, 11, 13, 17};

Collections.shuffle(Arrays.asList(array));

System.out.println(Arrays.toString(array));

}

}

BigDaddyLoveHandlesa at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 9
Indeed!
miss_eleca at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 10
> Indeed!Rather!!
BigDaddyLoveHandlesa at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 11
editednull
miss_eleca at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 12
<claps> Bravo! </claps>
_helloWorld_a at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...
# 13
You like me! You really like me!
BigDaddyLoveHandlesa at 2007-7-13 0:38:28 > top of Java-index,Java Essentials,Java Programming...