生成带有随机单词的字符串

时间:2012-12-04 10:43:27

标签: c# random permutation

我需要编写一个函数来随机化我的字符串中的一些单词。例如:

[Hello|Hi] guys. This is my [code|string]

该函数应该返回:

Hello guys. This is my code

Hi guys. This is my string

3 个答案:

答案 0 :(得分:5)

您可以获得这样的随机数生成器:

var rand = new Random();

至于解析你的字符串并获得所有选项,我建议你研究System.Text.RegularExpressions

到目前为止,其他答案刚刚展示了如果您已经为不同占位符提供了一个或两个选项,您将如何获得随机字符串。这些都很好,但写出来很无聊又乏味。编写一个可以像OP一样使用随机字符串“模板”的解析器,然后用它来生成随机字符串会好得多。

以下是我放在一起的快速内容:

using System;
using System.Text.RegularExpressions;

namespace StackOverLoadTest {
static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() {
        var s = new RandomString("[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week].");
        for (int i = 0; i < 10; i++)
            Console.WriteLine(s);
    }
}

public class RandomString {
    private Random _rnd = new Random();
    private static Regex _rex = new Regex(@"\[ ( \|?  (?<option>[^]|]+) )+ \]", System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
    string _template;
    public RandomString(string template) {
        _template = template;
    }
    public override string ToString() {
        return _rex.Replace(_template, GetRandomOption);
    }
    public string GetRandomOption(Match m) {
        var options = m.Groups["option"].Captures;
        int choice = _rnd.Next(0, options.Count);
        return options[choice].Value;
    }
}
}

如您所见,您使用模板创建一个新的RandomString对象。然后根据需要随时调用ToString()函数,每次获得选项的新随机排列。

您可以使用任意数量的占位符和任意数量的选项(0除外)。 我在这个例子中使用的字符串模板是:

"[Hey|Hi] guys. [I|You|We|He|She] should [walk] to the [park|field|farm] sometime [today|tomorrow|next week]."

运行上面的代码,我得到了以下结果:


Hey guys. I should walk to the park sometime today.
Hi guys. We should walk to the farm sometime today.
Hi guys. He should walk to the field sometime next week.
Hey guys. You should walk to the park sometime next week.
Hi guys. She should walk to the farm sometime next week.
Hey guys. We should walk to the field sometime tomorrow.
Hi guys. I should walk to the farm sometime today.
Hey guys. He should walk to the field sometime tomorrow.
Hi guys. You should walk to the park sometime next week.
Hi guys. I should walk to the farm sometime today.

答案 1 :(得分:0)

试试这个:

private string randArr(String[] _arr)
{
    Random _rnd = new Random(DateTime.Now.GetHashCode());
    return _arr[_rnd.Next(_arr.length)];
}

只需调用它为数组提供字符串值。像这样:

String.Format("{0} guys. This is my {1}", randArr(["Hello","Hi"]), randArr(["code","string"]));

答案 2 :(得分:0)

我使用了这种方法:

Random rand = new Random();
int val = rand.Next(0, 100);
Console.WriteLine(
    "{0} guys. This is my {1}",

    val >= 50 ? "Hi" : "Hello",
    val < 50 ? "code" : "string");

我给出了50%-50%的几率写出的单词,这就是>= 50&amp;你知道< 50你可以改变它。

如果你想将每个单词随机化为自己而不是完整的句子(上面的代码只给你2个变体),只需搞乱代码或者评论我修改它。

注意:

实际上不是50%-50%。我不想混淆,但如果你想这样,第一个条件应该是>= 49 *

条件(condition ? statement : statement)的语法称为 ternary if operator