vb.net - 从文件夹中随机选择 1000 个文件
我有一个文件夹,里面有很多文件。大约在 1000 到 15000 之间。
我需要从此文件夹中随机挑选 1000 个文件并将其复制到另一个文件夹。 我知道如何通过将文件列表插入到数组中然后随机选择一个来从文件夹中获取单个随机文件,但不知道如何获取多个文件,同时避免选择同一文件两次。
例如,如果我的文件夹中有 1001 个文件,那么获取前几个文件不会有任何问题,但到最后,很可能会选择一个已经复制过来的文件,并且需要多次尝试才能找到例如最后一个文件只是运气好...
我的第一个想法是将文件数除以 1000。例如 1500/1000 = 1.5。然后创建一个 0 到 1.5 之间的随机整数。这将返回 1。然后在第 n 个图像和第 n 个图像 * 1.5 之间执行下一个随机数。
如果文件夹有 15000 个文件,它会在 1 到 15 之间随机选择第一个文件,然后在 6 到 30 之间,依此类推。
但必须有一个更智能的解决方案。
感谢任何帮助
I have a folder that has a many files in it. somewhat between 1000 and 15000.
I would need to randomly pick 1000 files from this folder and copy it over to another folder.
I know how to get a single random file from a folder by inserting the list of files in an array and then selecting one randomly, but dont know how to get many while avoiding to select the same file twice.
If for example I have 1001 file in my folder, it will have no trouble getting the fist few, but then towards the end, it is very likely to pick a file that has already been copied over and it would take many tries to find for example the last file just by luck...
my first idea was to divide the number of files by 1000. so for example 1500/1000 = 1.5. and then create a random integer between 0 and 1.5. this would return 1. then do the next rand between nth image and nth image * 1.5.
if the folder hast 15000 files, it would pick the first file randomly between 1 and 15, and then between 6 and 30 and so on..
but there must be a smarter solution for this..
any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以随机排序它们:
这是使用
System.Linq
You can order them randomly:
This is using
System.Linq
使用文件列表的索引
Using indices into the list of files