小编Phi*_*lip的帖子

C#中的随机数生成器问题

很久以后,我设法让我的问题来自于我在第一个问题中询问的程序.它将一个随机数添加到列表中以用作ID号,然后将其导出到Excel.但是当我在我的数据文件中使用2个以上的数据成员时,我遇到了一个问题:我生成的随机数加倍,导致我的程序崩溃.

static Dictionary<string,Backup> getData()
{

    Dictionary<string, Backup> bDict = new Dictionary<string, Backup>();
    StreamReader reader = new StreamReader("/data/storedata.txt");

    while (!reader.EndOfStream)
    {

        string line = reader.ReadLine();
        string[] parts = line.Split(' ');

        string item = parts[0];
        string owner = parts[1];

        Random rnd = new Random();
        int test = rnd.Next(item.Length+10000);//For every 'item' a Random number is generated.(the +10000 is simply to produce a 4-digit number)

        //Console.WriteLine(test);//Testing 
        Backup BP = new Backup(item, owner,test);

        bDict.Add(test.ToString(), BP);//Adding to the Dictionary.

        //Console.WriteLine(string.Format("{0}, {1}, {2}", item, test, owner));

    } …
Run Code Online (Sandbox Code Playgroud)

c# random numbers

3
推荐指数
1
解决办法
370
查看次数

标签 统计

c# ×1

numbers ×1

random ×1