问:随机字符串没有重复c#

时间:2014-11-13 14:31:16

标签: c# repeat

我的代码有问题。我想要没有重复的字符串输出。我在这里呆了一个星期。

我已经尝试RemoveAdd,但我仍然收到错误。

这是我的代码

public void StringRandom()
{
    Random bsd = new Random();

    string[] femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                                "Abby", "Laila", "Sadie", "Olivia", 
                                "Starlight", "Talla" };
    int fIndex = bsd.Next(0, femalePetNames.Length);
    txttBox2.Text = femalePetNames[fIndex];
}

一个输出如下: laila ,悲伤, laila ,奥利维亚........(重复)

希望你们能给我任何帮助。感谢

更新---------------------------------------------- --------------------------------

我只是尝试Marty Thompson的解决方案,而且一些代码可能是litle错误。但我已经尝试修复它和YESSS输出有字符串随机而不重复。非常感谢Matty Thompson和你们所有人

这是新的正确代码

List<string> femalePetNames = new List<string> { "Maggie", "Penny", "Saya", "Princess", 
                    "Abby", "Laila", "Sadie", "Olivia", 
                    "Starlight", "Talla" };

private  void Button_Click(object sender, RoutedEventArgs e)
{
if (femalePetNames.Count > 0)
   {
    Random bsd = new Random();

    int fIndex = bsd.Next(0, femalePetNames.Count);
    txtbox.Text = femalePetNames[fIndex];
    femalePetNames.RemoveAt(fIndex);
   }
}

5 个答案:

答案 0 :(得分:2)

您最有可能想要一些定义集合的方法以及方法之外的Random。从您向我们展示的内容中确切地说并不明显,但重点是该集合必须在调用RemoveAt方法之间保持不变。

List<string> femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                                "Abby", "Laila", "Sadie", "Olivia", 
                                "Starlight", "Talla" };
Random bsd = new Random();

public void StringRandom()
{   
    if(femalePetNames.Length == 0)
    {
        // Do something here to handle when you've used all the pet names.
    }

    int fIndex = bsd.Next(0, femalePetNames.Length);
    txttBox2.Text = femalePetNames[fIndex];
    femalePetNames.RemoveAt(fIndex);
}

答案 1 :(得分:0)

你基本上要求的是阵列改组。您可以创建一个随机排序的IEnumerable,如下所示:

Random r;
// Make sure r is initialized properly at some point
femalePetNames.OrderBy( x=> r.Next() ) 

你应该probalby在你的方法之外声明你的数组。然后,您可以创建一个方法,每次调用它时,都会从随机排序的IEnumerable中提供名称:

string[] femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                                "Abby", "Laila", "Sadie", "Olivia", 
                                "Starlight", "Talla" };

IEnumerator<string> randomNameEnumerator;
Random r = new Random();

public string NonRepeatingName()
{
    if (randomNameEnumerator == null || !randomNameEnumerator.MoveNext())
    {
        randomNameEnumerator = femalePetNames.OrderBy( x=> r.Next()).GetEnumerator();
        randomNameEnumerator.MoveNext();
    }
    return randomNameEnumerator.Current;
}

然后,您可以将名称分配给文本框,如下所示:

void Main()
{
    textBox1.Text = NonRepeatingName();
    textBox2.Text = NonRepeatingName();
    textBox3.Text = NonRepeatingName();
    // ...
    textBoxN.Text = NonRepeatingName();
}

答案 2 :(得分:0)

您将不得不在此方法范围之外维护集合的状态。主要的变化是移动femalePetNames集合的声明并在输出后删除元素。

    List<string> femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                            "Abby", "Laila", "Sadie", "Olivia", 
                            "Starlight", "Talla" };
    public void StringRandom()
    {
        if (femalePetNames.Count > 0)
        {
            Random bsd = new Random();

            int fIndex = bsd.Next(0, femalePetNames.Count);
            txttBox2.Text = femalePetNames[fIndex];
            femalePetNames.RemoveAt(fIndex);
        }
    }

我还将其更改为List以使用易于使用的RemoveAt方法。如果由于某种原因这不是一个选项,您将需要查看如何从正常数组中删除项目。

答案 3 :(得分:0)

拿出你的名字并用FYK shuffle将它们洗牌(略有修改 - 这个版本需要一个数组并修改它):

    public static T[] Shuffle<T>(this T[] arr)
    {
        System.Random rng = new System.Random();
        int n = arr.Length;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = arr[k];
            arr[k] = arr[n];
            arr[n] = value;
        }
        return arr;
    }

测试代码:

        string[] femalePetNames = { "Maggie", "Penny", "Saya", "Princess", 
                            "Abby", "Laila", "Sadie", "Olivia", 
                            "Starlight", "Talla" };

        foreach (string s in femalePetNames.Shuffle())
        {
            Console.WriteLine(s);
        }

输出:

Starlight
Princess
Saya
Maggie
Penny
Laila
Talla
Olivia
Abby
Sadie

答案 4 :(得分:0)

我只是尝试Marty Thompson的解决方案,而且一些代码可能是litle错误。但我已经尝试修复它和YESSS输出有字符串随机而不重复。非常感谢Matty Thompson和你们所有人

这是新的正确代码

List<string> femalePetNames = new List<string> { "Maggie", "Penny", "Saya", "Princess", 
                        "Abby", "Laila", "Sadie", "Olivia", 
                        "Starlight", "Talla" };

    private  void Button_Click(object sender, RoutedEventArgs e)
    {
    if (femalePetNames.Count > 0)
       {
        Random bsd = new Random();

        int fIndex = bsd.Next(0, femalePetNames.Count);
        txtbox.Text = femalePetNames[fIndex];
        femalePetNames.RemoveAt(fIndex);
       }
    }