如何在C#中随机化文本?

huangapple go评论81阅读模式
英文:

How to randomise text in c#?

问题

我想创建一个C#程序,生成随机数,但我找不到方法。
到目前为止,我想出的代码如下:

using System;

namespace DigitalDice
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[1]");
        }
    }
}

我只需要一种生成随机单词的方法,我正在寻找的示例是如何随机生成两个不同的输出,然后我可以根据需要进行更改。

英文:

I want to make a c# program which generates random numbers, but I can't find how to.
The code I've come up with so far:

using System;

namespace DigitalDice
  {
      class Program
      {
        static void Main(string[] args)
       {
          Console.WriteLine("[1]");
      }
   }
}

I just need a way to make random words, the example I'm looking for is how to randomise with 2 different outputs, then I can change it to how I like it.

答案1

得分: 0

以下是代码的翻译部分:

> 我不确定你确切想要什么,但如果你想要**随机化单词**,你可以尝试以下代码

    public class Program {
        public static void Main(string[] args) {
            var res = Randomise("12345");
            Console.WriteLine(res);
            Console.ReadKey();
        }

        public static string Randomise(string input)
        {
            string res = string.Empty;
            Random random = new Random();
            while (!string.IsNullOrEmpty(input)) {
                int num = random.Next(input.Length);
                res += input.Substring(num, 1);
                input = input.Remove(num, 1);
            } 
            return res;
        }
    }
英文:

> I am not sure what you want exactly but if you are looking for
> randomizing words, you can try the following

public class Program {
    public static void Main(string[] args) {
        var res = Randomise("12345");
        Console.WriteLine(res);
        Console.ReadKey();
    }

    public static string Randomise(string input)
    {
        string res = string.Empty;
        Random random = new Random();
        while (!string.IsNullOrEmpty(input)) {
            int num = random.Next(input.Length);
            res += input.Substring(num, 1);
            input = input.Remove(num, 1);
        } 
        return res;
    }
}

答案2

得分: 0

你可以使用以下代码生成随机单词。

以下代码还可以帮助您定义随机单词的长度。

首先,创建一个方法来返回一个随机单词

/// <summary>
/// 指定要生成的随机单词的长度
/// </summary>
/// <param name="wordLength"></param>
/// <returns></returns>
static string RandomLetter(int wordLength)
{
    string randLetter = " ";
    string randWord = " ";

    string[] letters = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
        "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

    Random rnd = new Random();

    for (int s = 0; s < wordLength; s++)
    {
        // 生成一个新的随机数,范围在1到26之间
        int newRandomNumber = rnd.Next(0, 26);
        // 找到与该数字关联的字母
        randLetter = letters[newRandomNumber];
        randWord += randLetter;
    }
    // 返回没有空格的单词
    return randWord.Trim();
}

接下来,调用上述方法以获取一个随机单词

// 主方法
public static void Main(string[] args)
{
    Console.WriteLine(RandomLetter(5));
}

引用:代码片段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

class Program
{
    /// <summary>
    /// 指定要生成的随机单词的长度
    /// </summary>
    /// <param name="wordLength"></param>
    /// <returns></returns>
    static string RandomLetter(int wordLength)
    {
        string randLetter = " ";
        string randWord = " ";

        string[] letters = new string[26] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
            "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

        Random rnd = new Random();

        for (int s = 0; s < wordLength; s++)
        {
            // 生成一个新的随机数,范围在1到26之间
            int newRandomNumber = rnd.Next(0, 26);
            // 找到与该数字关联的字母
            randLetter = letters[newRandomNumber];
            randWord += randLetter;
        }
        // 返回没有空格的单词
        return randWord.Trim();
    }

    // 主方法
    public static void Main(string[] args)
    {
        Console.WriteLine(RandomLetter(5));
    }
}
英文:

You can generate Randomize words using below code.

Below code also helps you to define length of the random word.

First, Have a method to return a random word

/// &lt;summary&gt;
/// Pass legth of random word you need to generate
/// &lt;/summary&gt;
/// &lt;param name=&quot;wordLength&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
static string RandomLetter(int wordLength)

{
    string randLetter = &quot; &quot;;
    string randWord = &quot; &quot;;

    string[] letters = new string[26]  { &quot; a&quot;, &quot; b&quot;, &quot; c&quot;, &quot; d&quot;, &quot; e&quot;, &quot; f&quot;, &quot; g&quot;, &quot; h&quot;, &quot; i&quot;, &quot; j&quot;, &quot; k&quot;, &quot; l&quot;, &quot; m&quot;, &quot; n&quot;,

           &quot; o&quot;, &quot; p&quot;, &quot; q&quot;, &quot; r&quot;, &quot; s&quot;, &quot; t&quot;, &quot; u&quot;, &quot; v&quot;, &quot; w&quot;, &quot; x&quot; , &quot; y&quot;, &quot; z&quot;};

    Random rnd = new Random();

    for (int s = 0; s &lt; wordLength; s++)

    {
        //make newrandom to 26
        int newRandomNumber = rnd.Next(1, 26);
        //it finds the letter thas is associated to the number
        randLetter = letters[newRandomNumber];
        randWord += randLetter;
    }
    //it returns word without spaces
    return Regex.Replace(randWord, @&quot;\s+&quot;, &quot;&quot;);
}

Next, Call the above method to get a random word

///Main method
public static void Main(string[] args)
{
    Console.WriteLine(RandomLetter(5));
}

Ref: Code snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

class Program

{
    /// &lt;summary&gt;
    /// Pass legth of random word you need to generate
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;wordLength&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    static string RandomLetter(int wordLength)

    {
        string randLetter = &quot; &quot;;
        string randWord = &quot; &quot;;

        string[] letters = new string[26]  { &quot; a&quot;, &quot; b&quot;, &quot; c&quot;, &quot; d&quot;, &quot; e&quot;, &quot; f&quot;, &quot; g&quot;, &quot; h&quot;, &quot; i&quot;, &quot; j&quot;, &quot; k&quot;, &quot; l&quot;, &quot; m&quot;, &quot; n&quot;,

               &quot; o&quot;, &quot; p&quot;, &quot; q&quot;, &quot; r&quot;, &quot; s&quot;, &quot; t&quot;, &quot; u&quot;, &quot; v&quot;, &quot; w&quot;, &quot; x&quot; , &quot; y&quot;, &quot; z&quot;};

        Random rnd = new Random();

        for (int s = 0; s &lt; wordLength; s++)

        {
            //make newrandom to 26
            int newRandomNumber = rnd.Next(1, 26);
            //it finds the letter thas is associated to the number
            randLetter = letters[newRandomNumber];
            randWord += randLetter;
        }
        //it returns word without spaces
        return Regex.Replace(randWord, @&quot;\s+&quot;, &quot;&quot;);
    }
    
    ///Main method
    public static void Main(string[] args)
    {
        Console.WriteLine(RandomLetter(5));
    }

}

huangapple
  • 本文由 发表于 2020年1月3日 14:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定