英文:
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
/// <summary>
/// Pass legth of random word you need to generate
/// </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++)
{
//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, @"\s+", "");
}
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
{
/// <summary>
/// Pass legth of random word you need to generate
/// </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++)
{
//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, @"\s+", "");
}
///Main method
public static void Main(string[] args)
{
Console.WriteLine(RandomLetter(5));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论