using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tests
{
class RandomTests
{
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
static List<string> listv;
public static void GenerateRandomStrings()
{
for (int i = 0; i < random.Next(20, 1000); i++)
{
string rand = RandomString(20);
ListViewCostumControl.lvnf.Items.Add(rand);
}
listv = ListViewCostumControl.lvnf.Items.Cast<ListViewItem>()
.Select(item => item.Text)
.ToList();
}
private void UsageExample()
{
GenerateRandom(5, 1, 6);
}
public static List<int> GenerateRandom(int count)
{
return GenerateRandom(count, 0, Int32.MaxValue);
}
// Note, max is exclusive here!
public static List<int> GenerateRandom(int count, int min, int max)
{
// initialize set S to empty
// for J := N-M + 1 to N do
// T := RandInt(1, J)
// if T is not in S then
// insert T in S
// else
// insert J in S
//
// adapted for C# which does not have an inclusive Next(..)
// and to make it from configurable range not just 1.
if (max <= min || count < 0 ||
// max - min > 0 required to avoid overflow
(count > max - min && max - min > 0))
{
// need to use 64-bit to support big ranges (negative min, positive max)
throw new ArgumentOutOfRangeException("Range " + min + " to " + max +
" (" + ((Int64)max - (Int64)min) + " values), or count " + count + " is illegal");
}
// generate count random values.
HashSet<int> candidates = new HashSet<int>();
// start count values before max, and end at max
for (int top = max - count; top < max; top++)
{
// May strike a duplicate.
// Need to add +1 to make inclusive generator
// +1 is safe even for MaxVal max value because top < max
if (!candidates.Add(random.Next(min, top + 1)))
{
// collision, add inclusive max.
// which could not possibly have been added before.
candidates.Add(top);
}
}
// load them in to a list, to sort
List<int> result = candidates.ToList();
// shuffle the results because HashSet has messed
// with the order, and the algorithm does not produce
// random-ordered results (e.g. max-1 will never be the first value)
for (int i = result.Count - 1; i > 0; i--)
{
int k = random.Next(i + 1);
int tmp = result[k];
result[k] = result[i];
result[i] = tmp;
}
return result;
}
}
}
目前,我仅在Form1中使用GenerateRandomStrings()。但是现在我要生成具有随机内容和大小的随机文本文件。例如具有随机内容的100MB或100GB文件大小。
文件大小应从字节到千兆字节。可以是1b文件或100GB文件。
然后像在GenerateRandomStrings()函数中一样将文件添加到列表视图中。
ListViewCostumControl.lvnf.Items.Add(rand);
添加具有随机内容和大小的文本文件的相同想法。
此代码创建一个文件,该文件的大小由用户定义,而填充的内容为随机文本,
Console.WriteLine("Give file size in bytes");
var fileSize = int.Parse(Console.ReadLine());
var buffer = new byte[fileSize];
new Random().NextBytes(buffer);
var text = System.Text.Encoding.Default.GetString(buffer);
File.WriteAllText("/home/path/to/file/randomtext.txt", text);
注意:我正在使用Linux,因此您想更改该路径。