这是一个函数,使阵列和从填充它LoLim
,直到HiLim
再混洗的顺序:
static Random random = new Random(); //these two statics are to make a random number, i need the first static to be outside the function so it won't keep making the same random number
static int RandomNum(int LoLim, int HiLim, int index)
{
var nums = Enumerable.Range(LoLim,HiLim).ToArray(); //these next lines make an array from LoLim to HiLim and then shuffle it
for (int i = 0; i < nums.Length; i++)
{
int randomIndex = random.Next(nums.Length);
int temp = nums[randomIndex];
nums[randomIndex] = nums[i];
nums[i] = temp;
}
return nums[index];
然后,我有这个功能,它使一个二维数组,然后打印它。它使用RandomNum
但不是按照我想要的方式使用,但是我不知道如何使其工作。
static Array Matrix(int Rows, int Columns) //this is a function to initiate and print the array
{
int[,] LotteryArray = new int[Rows, Columns];
for (int i = 0; i < LotteryArray.GetLength(0); i++) //this is a series of loops to initiate and print the array
{
for (int j = 0; j < LotteryArray.GetLength(1); j++)
{
LotteryArray[i, j] = RandomNum(1,46,j); //the numbers are supposed to be between 1 and 45, so i tell it to do it until 46 because the upper limit is exclusive
Console.Write("{0,3},", LotteryArray[i, j]); //the {0,3} the 3 is three spaces after the first variable
}
Console.WriteLine();
}
return LotteryArray;
基本上,我希望它可以调用RandomNum
每个“行”,以便可以对数组进行混洗,然后再将其拉出nums[index]
并打印出来。我想要这样做的原因是这样,我可以在每一行中使用非重复的随机数。例如:我不希望一行为“ 21、4、21、5、40、30”。
即使代码有效,您的代码也会出现各种问题。“逻辑”问题是:
Random
数字生成器,而应使用RNGCryptoServiceProvider
(参见http://www.cigital.com/papers/download/developer_gambling.php,但是请注意,这是问题比其他问题要少得多...如果生成的混洗都是等概率的,我们可以使用“随机性稍差”的混洗生存下来)Enumerable.Range()
您的编码问题有所不同:您必须将经过改组的行保存在某个地方,然后获取第一列的值。
在这里,我正在使用一个类,通过改组来封装行
public class ShuffledRow
{
public static readonly Random Random = new Random();
public readonly int[] Row;
/// <summary>
/// Generates and shuffles some numbers
/// from min to max-1
/// </summary>
/// <param name="min"></param>
/// <param name="max">Max is excluded</param>
public ShuffledRow(int min, int max)
{
int count = max - min;
Row = Enumerable.Range(min, count).ToArray();
Shuffle(Row);
}
private static void Shuffle<T>(T[] array)
{
// Fisher-Yates correct shuffling
for (int i = array.Length - 1; i > 0; i--)
{
int j = Random.Next(i + 1);
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
另一个问题:除非您知道自己在做什么,否则请不要在C#中使用多维数组。可悲的是,他们是.NET的一个混蛋而被遗忘的孩子。使用锯齿状数组(数组数组)。
public static int[][] Matrix(int rows, int columns)
{
int[][] lottery = new int[rows][];
for (int i = 0; i < lottery.Length; i++)
{
ShuffledRow sr = new ShuffledRow(1, 46);
lottery[i] = sr.Row;
Array.Resize(ref lottery[i], columns);
Console.WriteLine(
string.Join(",", lottery[i].Select(
x => string.Format("{0,3}", x))));
}
return lottery;
}
public static int[][] Matrix(int rows, int columns)
{
int[][] lottery = new int[rows][];
for (int i = 0; i < lottery.Length; i++)
{
ShuffledRow sr = new ShuffledRow(1, 46);
lottery[i] = new int[columns];
for (int j = 0; j < columns; j++)
{
lottery[i][j] = sr.Row[j];
Console.Write("{0,3},", lottery[i][j]);
}
Console.WriteLine();
}
return lottery;
}
我准备了Matrix函数的两个版本,一个与您正在使用的版本更相似,一个与LINQ和“高级”版本更相似。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句