-2

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

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

该函数应返回:

Hello guys. This is my code

或者

Hi guys. This is my string

4

3 回答 3

6

你可以像这样得到一个随机数生成器:

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.

于 2012-12-04T10:46:54.767 回答
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"]));
于 2012-12-04T10:49:06.793 回答
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&< 50你看到的。你可以改变它。

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

笔记:

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

条件 ( condition ? statement : statement) 的语法称为三元 if 运算符

于 2012-12-04T10:56:13.610 回答