我有一个函数,可以创建数组并对其进行混洗,但是我不知道如何以我想要的方式调用它

松饼机

这是一个函数,使阵列和从填充它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”。

Xanatos

即使代码有效,您的代码也会出现各种问题。“逻辑”问题是:

  • 您正在使用偏向混洗算法。您应该使用Fisher-Yates进行正确的改组(请参阅http://blog.codinghorror.com/the-danger-of-naivete/
  • 如果您确实想进行“良好的”改组,则不应使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我已经制作了一个登录系统,但是,我不知道如何设置其中的某些样式

我创建了一个库存系统,但我不知道如何进行

我有一个代码,其中调用了Lateinit变量后对其进行了初始化,但我不知道如何

我在调用构造函数时有一个错误(我认为)不知道如何解决它

我有一个论点问题,我不知道为什么吗?

我是否有一个空函数返回,使用python代码模块时我不知道?

我已经通过react.js类组件创建了一个计数器,但是它没有增加,我不知道它是怎么了

所以我做了一个音乐机器人,但是我不知道这是不和谐的错误

我可以在不知道动态数组大小的情况下对其进行迭代

我想做一个计数对象,但我不知道如何解决

我在pygame中使Checkers以我想要移动的方式是单击2次鼠标,但是我不知道该怎么做

我不知道另外一个来自哪里

我想调用一个stock.setPrice()方法,但是我不知道如何

尝试通过while循环获取所有数据,但是已经获取了第一个数据,不知道如何更改我的代码。(Php)

在Ruby中:我在调用方法时会给它一个参数,但是不知道在哪里。我认为这与块绑定有关?

我创建了一个压缩器,但是存档已损坏,我也不知道为什么

当我不知道签名时如何调用函数?

我不知道如何在另一个函数中调用先前创建的函数?

不知道如何传递图像的 id 以便我可以在我的 ajax 调用中使用它

我创建了一个后缀数组,但我不知道这段代码有什么问题

如何以我想要的方式在 jQuery 中使用“attr()”?

我一直有一个关于 .nib 文件的错误,我不知道它是如何创建的

我想创建一个输出,但不知道如何

我不知道如何使用数组进行划分

我不知道如何在 xamarin 上连续运行一个函数

我在 Flutter 中有一个错误,我不知道如何解决

我不知道如何返回并打印函数结果。(我在做一个python通讯录程序)

单击一个我不知道 ID 的按钮

R函数,我的as.date有错误吗?我收到一个错误,不知道如何将 df 转换为类