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

孤街浪徒 提交于 2020-03-26 06:44:27

问题


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?


回答1:


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.




回答2:


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>).




回答3:


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:

  • http://www.cplusplus.com/reference/string/string/
  • http://www.cplusplus.com/reference/fstream/ifstream/
  • http://www.cplusplus.com/reference/istream/istream/getline/
  • http://www.cplusplus.com/reference/vector/vector/
  • http://www.cplusplus.com/reference/list/list/
  • http://www.cplusplus.com/reference/cstdlib/rand/
  • Bonus: http://www.cplusplus.com/reference/algorithm/random_shuffle/


来源:https://stackoverflow.com/questions/56927252/what-is-the-best-way-to-randomly-pick-a-record-line-from-a-txt-or-csv-file-every

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!