Pick random objects from array/arraylist. Newbie.
Hey all!
The only thing left that bugs me in my into java class is this - picking random objects from arrays or arraylists or wherever. After that I'm sailing free and moving to more advanced stuff 8->
Alright people, please give me tips or hints about this thing, as I really want to improve my object to int/string/double/etc. skills. The bottom line is the use of Random generator - not Math.methods or similar. So how can I tune this thing?
Assume following thing, example with objects of Employee class.
Code with comments:
Code Java:
import java.util.*;
import java.util.Random;
class Employee{
private int salary;
public void setSalary(int s){
s= salary;
}
public int getSalary(){
return salary;
}
}
class rand{
public Employee [] get(Employee [] arr){
Employee [] arr2 = new Employee [3]; //three (or whatever) values.
Random rand = new Random(); //setting up the Random generator - How to with Objects??
//unique numbers can be fixed through while loops by adding to the new array manually - no problem here.
}
}
public class Job{
public static void main(String [] args){
Employee [] list = new Employee [5]; //Lets say 5 (or whatever).
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
Employee e4 = new Employee();
Employee e5 = new Employee();
e1.setSalary(10000);
e2.setSalary(2000);
e3.setSalary(15000);
e4.setSalary(18000);
e5.setSalary(23000);
list[0] = e1;
list[1] = e2;
list[2] = e3;
list[3] = e4;
list[4] = e5;//Yep, doing it with lots of code :)
//Here I will call on the class rand.get(list) - so it will make wonders and I can do whatever I want with the new array (or whatever) :P
}
}
Any suggestions will be greatly appreciated! But people, focus on the Random generator please, I know that there are other methods that will work besides this.
Re: Pick random objects from array/arraylist. Newbie.
To get a random int between 0 and n-1, multiply the next floating point random number times n, casting to an int and getting its absolute value. To use this to find a random object in an array or list, n would be the size of said array or list.
Re: Pick random objects from array/arraylist. Newbie.
copeg
Your answer was a little advanced for me, but I took the bits and working on it.. Thank you! Here's what I've come up with. Example of a lottery drawing:
Code:
Code Java:
import java.util.*;
import java.util.Random;
import java.util.ArrayList;
class Ticket{
private int id;
public void setid(int i){
i=id;
}
public int getid(){
return id;
}
}
class pickRand{
public static Ticket [] get(ArrayList <Ticket> jipp){
Ticket [] rst = new Ticket [5];
Random rand = new Random();
for(int i=0;i<rst.length;i++){
Ticket uniq = jipp.remove(rand.nextInt(jipp.size()));
rst[i] = uniq;
}
return rst;
}
}
public class Account{
public static void main(String []args){
int[] id = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
ArrayList <Ticket> drw = new ArrayList <Ticket>();
for(int i = 0; i < id.length; i++) {
drw.add(new Ticket());
drw.get(0).setid(id[i]);
}
Ticket [] results = pickRand.get(drw);
System.out.println(results[0] + " " + results[1] + " " + results[2] + " " + results[3] + " " + results [4]);
}
}
It does return random Tickets, I still od wonder if can fix that rand.sentence in another way...
Re: Pick random objects from array/arraylist. Newbie.
After wrapping my head around the problem I've come up with the solution for unique objects using random generator:
Code :
class pickRand{
public static Ticket [] get(ArrayList <Ticket> jipp){
Ticket [] rst = new Ticket [5];
Random rand = new Random();
for(int i=0;i<rst.length;i++){
Ticket uniq = jipp.remove(rand.nextInt(jipp.size()));
int a = 0;
do{
a=0;
for(int j =0;j<i;j++){
if(rst[j].equals(uniq)){
a =1;
}
}
}while(a==1);
rst[i] = uniq;
}
return rst;
}
}
Hope this helps for someone who's looking for answers. The main thing is to check the array, and this can be done in several different ways, but here's one for ya.
This can also be made FAR more simpler using shuffle or Math classes/objects.
Thanks for feedback! :rolleyes: