txcipack
txcipack

Reputation: 1

What is the best way to randomly pick a record/line from a txt or CSV file every time the user clicks a button?

Codeless at the moment, just trying to figure it out to start my project. Say for example I'm creating a dare game. Every time a user clicks the dare button, it should randomly pick a dare from the dare file, either a txt or CSV and display it in a field.

Best way to accomplish this?

Upvotes: 0

Views: 344

Answers (3)

Peter O.
Peter O.

Reputation: 32878

Choosing a line from a file, uniformly at random, can be done using the reservoir sampling technique. For each line in the file, choose it at a 1/N chance, where N is the number of lines read so far, including the line just read. The random line is then the last line chosen this way.

Upvotes: 3

Mark Melgo
Mark Melgo

Reputation: 1478

I would suggest you read all the contents of the file and save it into an object. After that generate a random number every time the user clicks a button. The simplest way is to store it into a list of Strings (List<String>) but if you have other data that needs to be stored, example you have the dare and the corresponding sanction if the user doesn't complete the dare then you can make an object and store it to the object. Example:

public class Dare {
    String dare;
    String sanction;
    // add more attributes if needed

    // constructors and getters and setters below

}

Then you could have a list of Dare (List<Dare>).

Upvotes: 2

Jason C
Jason C

Reputation: 40336

In order to make sure every item has an even chance of being picked, you need, at minimum, to know the number of items (lines, in your case) at the time you generate the random number.

There are three general options to achieve this:

  • Load all of your data from your data files first and store it all in some kind of list. Then, you will have the count and items available, or

  • Store information about the number of items at the beginning of the file, so you can at least read the item count and choose the line number. Then you'd have to read the correct line after choosing a number, or

  • Ensure every record (line) in the file has a fixed length, and calculate the number of lines (and offset to the start of each) based on the file size and known line length.

There's different situations that call for each option but in your case option 1 makes the most sense (and option 3 makes the least). I'll leave the details as an exercise to you but:

  1. Load each line from the file, storing in a list.
  2. Choose a random number n, in [0, length of list).
  3. Then, you can readily access the nth item in the list.

As an alternative, if you'd like to make sure you go through the entire list and pick each item exactly once, another approach is:

  1. Load each line from the file, storing in a list.
  2. Randomly shuffle the items in the list.
  3. Go through the randomly shuffled list, in order.

Here's some relevant resources:

Upvotes: 1

Related Questions