如何让我的程序生成一个新的随机数,直到它找到一个以前没有使用过的?

时间:2017-01-19 10:59:20

标签: c#

好吧,标题可能有点误导,但我无法为我的问题找到更好的解释。我试图创建一个程序,它接受一个名字数组和一个姓氏数组,并将它们放在一个全名数组中。这里的问题是我试图确保相同的名称不会出现两次,因此如果firstname [randomindex]和lastname [randomindex]的组合已经在全名数组中,它将会重新滚动用于索引的随机数,直到它找到一个尚未使用的组合。到目前为止,我有点坚持这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _100randompersons
{
class Fyldefylde
{
    private string[] firstnames;
    private string[] lastnames;
    private string[] fullnames;
    private Random random;

    public Fyldefylde()
    {
        firstnames = new string[10] { "Kim", "Allan", "Frank", "Lone", "Line", "Anne", "Per", "Bo", "Grethe", "Mette" };
        lastnames = new string[10] { "Pedersen", "Nielsen", "Hansen", "Larsen", "Nygaard", "Harboe", "Bendix", "Højris", "Pilgaard", "Nyager" };
        fullnames = new string[100];
        random = new Random();
    }

    public string[] getFirstNames()
    {
        return firstnames;
    }

    public string[] getLastNames()
    {
        return lastnames;
    }

    public string[] getFullNames()
    {
        return fullnames;
    }

    public int getRandomIndex(int min, int max)
    {
        int randomnumber = random.Next(min, max);
        return randomnumber;
    }

    public void fillFullNames()
    {
        for(int i = 0; i < fullnames.Length; i++)
        {
            getRandomIndex(0, 10);
            while(fullnames.Contains(firstnames[getRandomIndex(0,10)] + " " + lastnames[getRandomIndex(0,10)]))
                {
                fullnames[i] = firstnames[getRandomIndex(0, 10)] + " " + lastnames[getRandomIndex(0, 10)];
                }
        }
    }

}
}

提前致谢! :)

3 个答案:

答案 0 :(得分:3)

看起来你只想制作全名的所有组合,为什么不这样做呢?

for (int i = 0; i < firstnames.Length; i++)
{
    for (int j = 0; j < lastnames.Length; j++)
    {
        fullnames[i*lastnames.Length + j] = firstnames[i] + " " + lastnames[j];
    }
}

然后,如果您希望以随机顺序输入名称,请运行一个方法来随机化名称的顺序

如果您希望以随机顺序排列名称,那么您可以随时执行以下操作

string[] firstnames = new string[10] { "Kim", "Allan", "Frank", "Lone", "Line", "Anne", "Per", "Bo", "Grethe", "Mette" };
string[] lastnames = new string[10] { "Pedersen", "Nielsen", "Hansen", "Larsen", "Nygaard", "Harboe", "Bendix", "Højris", "Pilgaard", "Nyager" };
string[] fullnames = new string[100];

List<int> indices = new List<int>();
for (int i = 0; i < firstnames.Length * lastnames.Length; i++)
{
    indices.Add(i);
}

Random random = new Random();
for (int i = 0; i < firstnames.Length; i++)
{
    for (int j = 0; j < lastnames.Length; j++)
    {
        int randomIndex = random.Next(indices.Count() - 1);
        fullnames[indices[randomIndex]] = firstnames[i] + " " + lastnames[j];
        indices.RemoveAt(randomIndex);
    }
}

答案 1 :(得分:2)

您可以使用简单的HashSet来确保值是唯一的:

    public void fillFullNames()
    {
        HashSet<string> newFullNames = new HashSet<string>();
        while (newFullNames.Count != fullnames.Length) {
            newFullNames.Add(firstnames[getRandomIndex(0, 10)] + " " + lastnames[getRandomIndex(0, 10)]);
        }
        fullnames = newFullNames.ToArray();
    }

答案 2 :(得分:1)

您最好的选择是生成<script type="text/javascript"> window.onload = function() { var elem = document.getElementById("yourElement"); document.body.appendChild(elem); }; </script> 的原型版本,代表每种可能的组合,如果您想要随机输出,则将其改组:

fullname

然后,您可以连续读取这个新数组,以获得没有重复数据的随机样本。