• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Return uniqe value at random from DataSet

Hi all
I have this piece  of code below..
I need a method that will return a random value
from the dataSet. (which contains 100 records.. )

If possible a second method should prevent dupplicate being returned..
I am thinking of using HashTable . but I am still working on it
The csv look like this

New;     High        ;Medium;    End;   Year;Index
1.55946;1.585132;1.78814;1.8174;1901;1
1.239951;1.331009;1.411654;1.473569;1902;2
1.292558;1.225041;1.239066;1.208609;1903;3
1.265997;1.261039;1.274634;1.306225;1904;4

Thanks in Advance
public static String readLine(String inName)

    throws FileNotFoundException, IOException {

        BufferedReader is = new BufferedReader(new FileReader(inName));

        String line = null;

        line = is.readLine( );

        is.close( );

        return line;

    }

Open in new window

0
ZURINET
Asked:
ZURINET
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
objectsCommented:
to use get a random entry you can use:

List<Double> list = new ArrayList<Double>();
BufferedReader in = new BufferedReader(new FileReader(filename));
String line = in.readLine();
while (null!=(line=in.readLine())) {
   for (String numbers : line.split(";") {
      list.add(Double.parseDouble(number));
   }
}
in.close();
Double random = list.get(wheel.nextInt(list.size()));
0
 
objectsCommented:
to avoid dupes use a Set instead of a List
0
 
objectsCommented:
something like:

Set<String> list = new HashSet<String>();
BufferedReader in = new BufferedReader(new FileReader(filename));
String line = in.readLine();
while (null!=(line=in.readLine())) {
   for (String number : line.split(";") {
      list.add(number);
   }
}
in.close();
double random = Double.parseDouble(list.toArray()[wheel.nextInt(list.size()].toString());
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:

Do you mean somthing like that:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;

public class RandomLine {
     ArrayList al;

    public RandomLine () {
        al = new ArrayList();
        try{
            String line = null;



            DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
            while((line = in.readLine()) != null){
                al.add(line);


            }
            in.close();






        } catch(Exception ex){
            System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }


public String getRandomLine() {
       int i = (int)(Math.random()*100);

    return (String) al.get(i);
}
    public static void main(String [] args){
     RandomLine rl =    new RandomLine();
        System.out.println(rl.getRandomLine());

    }


}

Open in new window

0
 
for_yanCommented:

This will avoid duplicates
(I am thinking that you need to return the whole line)
according to your example
and because your numbers are not uniform


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;

public class RandomLine {
     ArrayList al;

    public RandomLine () {
        al = new ArrayList();
        try{
            String line = null;



            DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
            while((line = in.readLine()) != null){
                if(!al.contains(line))al.add(line);


            }
            in.close();






        } catch(Exception ex){
            System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }


public String getRandomLine() {
       int i = (int)(Math.random()*100);

    return (String) al.get(i);
}
    public static void main(String [] args){
     RandomLine rl =    new RandomLine();
        System.out.println(rl.getRandomLine());

    }


}

Open in new window

0
 
for_yanCommented:

corrected - without duplicates:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;

public class RandomLine {
     ArrayList al;

    public RandomLine () {
        al = new ArrayList();
        try{
            String line = null;



            DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
            while((line = in.readLine()) != null){
                if(!al.contains(line))al.add(line);


            }
            in.close();






        } catch(Exception ex){
            System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }


public String getRandomLine() {
       int i = (int)(Math.random()*al.size());

    return (String) al.get(i);
}
    public static void main(String [] args){
     RandomLine rl =    new RandomLine();
        System.out.println(rl.getRandomLine());

    }


}

Open in new window

0
 
ZURINETAuthor Commented:
Sorry..
Just forget..
The Index ,, column enforce the uniqueness
0
 
for_yanCommented:

This code is tested:
(it will ensure uniqueness, nevertheless)
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;

public class RandomLine {
     ArrayList al;

    public RandomLine () {
        al = new ArrayList();
        try{
            String line = null;



            DataInputStream in = new DataInputStream(new FileInputStream("file.txt"));
            while((line = in.readLine()) != null){
                if(!al.contains(line))al.add(line);


            }
            in.close();






        } catch(Exception ex){
            System.out.println(ex.toString());
            ex.printStackTrace();
        }
    }


public String getRandomLine() {
       int i = (int)(Math.random()*al.size());

    return (String) al.get(i);
}
    public static void main(String [] args){
     RandomLine rl =    new RandomLine();
        System.out.println(rl.getRandomLine());

    }


}

Open in new window


Ouput:
1.265997;1.261039;1.274634;1.306225;1904;4

Open in new window

0
 
CEHJCommented:
Create a Set of beans from the file, say Set<PriceData>
0
 
ZURINETAuthor Commented:
Hi objects
I mean random row.. not random value..
0
 
CEHJCommented:
So you don't mind getting the same value twice or more?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now