如何在C#中只生成一次随机数 [英] How to generate a random number only once in C#
问题描述
首先,我要感谢Code Project帮助我学习C#。我开始学习该程序作为一种爱好并选择了c#,因为我听说对于没有任何编程背景的人来说这很容易学习。但是,我意识到这对我来说仍然太难了。但Code Project一直帮助我解决问题。无论如何...
所以我试图编写一个在控制台上显示动画的C#控制台应用程序。到目前为止,我能够编写大约95%的代码并且它正在工作但我仍然无法弄清楚如何仅从数字列表中生成一个随机数。我已经包含了我编写的代码供大家查看。现在我必须使用冗余方法来确保所有字符都打印在控制台上。这耗费了大量的时间,我猜也记忆犹新。以下是我的代码。如果代码看起来很难读,请原谅我。自从我开始使用c#以来只有大约1.5个月。
如果有人能告诉我我能做些什么从我的代码中删除多余的迭代编号然后我想它会完整。
First of all, I would like to thank Code Project for helping me out in my quest to learn C#. I started learning to the programme as a hobby and picked c# as I heard that it is quite easy to learn for a person without any programming background. However, I realised that it is still too tough for me. But Code Project has always helped me out in figuring things out. Anyways...
So I was trying to write a C# console app that displays animation on the console. So far I'm able to write about 95% of the code and it is working but I'm still not able to figure out how to generate a random number from list of number only once. I have included the code I have written for everyone to review. Right now I have to use a redundant method for making sure that all the characters are printed on the console. Which consumes a lot of time and i guess memory too. Below is my code. Please excuse me if the code seems hard to read as It is been only about 1.5months since when i started with c#.
If anyone can suggest me what I can do to remove the redundant Iteration number from my code then I guess it will be complete.
<pre>
using System;
using System.IO;
using System.Threading;
namespace BeautifulNameAnimation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please play in maximised window mode. PRESS ENTER after maximising your window");
Console.ReadKey();
Console.WriteLine("Please enter the complete path of your text file including the text file name with extension: ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(">>> ");
Console.ResetColor();
string path = Console.ReadLine();
Console.WriteLine("Provide animation delay number. Bigger the number Slower the animation.");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(">>> ");
Console.ResetColor();
int animDelay = int.Parse(Console.ReadLine());
Console.WriteLine("Redundancy Number: (This number decides the iteration for animation) -- Start with 1000 and then gradually increase it. ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(">>> ");
Console.ResetColor();
int redundencyCounter = int.Parse(Console.ReadLine());
Console.Clear();
//// Program Execution Stage -1
int[] Method1Output = ReturnColsAndRows(path);
//// Program Execution Stage -2
char[,] Method2Output = ExtractAllChars(Method1Output, path);
//// Program Execution Stage -3 (Final OutPut)
PrintTextFile(Method2Output, animDelay, redundencyCounter);
Console.ResetColor();
Console.ReadKey();
}
//Method No. 1
#region Method for calculating Rows and Colums in the Source Text File.
public static int[] ReturnColsAndRows(string path)
{
StreamReader reader = new StreamReader(path);
int cols = reader.ReadLine().Length;
int rows = 1;
string line = reader.ReadLine();
while (line != null)
{
rows++;
line = reader.ReadLine();
}
reader.Close();
int[] rowsNcols = new int[2] { rows, cols }; //To be used in Method No. 2 for Char[,]
return rowsNcols;
}
#endregion
//Method No. 2
#region Method for extracting individual characters from Source Text File.
public static char[,] ExtractAllChars(int[] Method1Output, string path)
{
StreamReader reader = new StreamReader(path);
string line = reader.ReadLine();
char[,] charsInSourceFile = new char[Method1Output[0], Method1Output[1]];
for (int row = 0; row < charsInSourceFile.GetLength(0); row++)
{
for (int col = 0; col < charsInSourceFile.GetLength(1); col++)
{
charsInSourceFile[row, col] = line[col];
}
line = reader.ReadLine();
}
reader.Close();
return charsInSourceFile;
}
#endregion
//Method No. 3
#region Method for printing extracted chars in a random fashion
public static void PrintTextFile(char[,] charsInSourceFile, int animDelay, int redundencyCounter)
{
int counter = 0;
Random rand = new Random();
while (true)
{
int row = rand.Next(charsInSourceFile.GetLength(0));
int col = rand.Next(charsInSourceFile.GetLength(1));
Console.SetCursorPosition(col, row);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(charsInSourceFile[row, col]);
counter++;
Thread.Sleep(animDelay);
if (counter == redundencyCounter) // This is the problem
{
Console.SetCursorPosition(0, charsInSourceFile.GetLength(0) + 2);
break;
}
}
}
#endregion
}
}
我的尝试:
我试图通过无法找到任何解决方案谷歌我的问题
What I have tried:
I tried to google my question by not able to find any solution
推荐答案
我仍然无法弄清楚如何仅从数字列表中生成一个随机数。
I'm still not able to figure out how to generate a random number from list of number only once.
'只有一次'make你的号码不是随机的,因为随机意味着重复的可能性。
你正在寻找'随机播放',就像玩纸牌一样。
在电脑上随机播放:
创建一个大小为52的数组,并将每个卡存储在数组中。卡片位于0到51位置。
在0到51之间绘制一个数字,将该卡与位置0处的1交换。
绘制1到51之间的数字,将该卡与位置1处的1交换。
在2到51之间绘制一个数字,将该卡与位置2处的1交换。
在3和之间绘制一个数字51,将该卡与位置3处的1交换。
依此类推,直到只剩下最后一张卡。
然后你根据需要顺序读取数组,你将在数组结束之前不要抽两次相同的卡片。
The 'only once' make your number not random, because random imply the possibility of repeat.
You are looking for 'shuffle', just like with playing cards.
Shuffle on computer:
make an array of size 52 and store each card in the array. Cards are at positions 0 to 51.
Draw a number between 0 and 51, swap that card with the 1 at position 0.
Draw a number between 1 and 51, swap that card with the 1 at position 1.
Draw a number between 2 and 51, swap that card with the 1 at position 2.
Draw a number between 3 and 51, swap that card with the 1 at position 3.
and so on until only last card remain.
Then you read the array in order as you need, you will not draw the same card 2 times until the end of array.
这篇关于如何在C#中只生成一次随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!