Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用不同行/列中的唯一随机数填充数组_C#_Arrays_Random_Integer_Autofill - Fatal编程技术网

C# 如何用不同行/列中的唯一随机数填充数组

C# 如何用不同行/列中的唯一随机数填充数组,c#,arrays,random,integer,autofill,C#,Arrays,Random,Integer,Autofill,我已经环顾了很多地方,但似乎找不到满意的答案。我想用唯一的数字填写5x5。在第1列中,它只能是介于1-5和第2列中的数字16-30,依此类推,直到第5列中的数字介于61-75之间。这是一个宾果棋盘。我尝试了一些方法,但没有一种有效,我似乎找不到一种方法用我的规范填充阵列。无论我在哪里看到的,都只是带有几个数字的预填充数组的示例,或者代码对我来说是高级的 我不久前创建了一个程序,它创建一个数字为1-75的规则数组,然后用fisher/yates算法将其洗牌。这是我应该继续使用还是从头开始?在此处输

我已经环顾了很多地方,但似乎找不到满意的答案。我想用唯一的数字填写5x5。在第1列中,它只能是介于1-5和第2列中的数字16-30,依此类推,直到第5列中的数字介于61-75之间。这是一个宾果棋盘。我尝试了一些方法,但没有一种有效,我似乎找不到一种方法用我的规范填充阵列。无论我在哪里看到的,都只是带有几个数字的预填充数组的示例,或者代码对我来说是高级的

我不久前创建了一个程序,它创建一个数字为1-75的规则数组,然后用fisher/yates算法将其洗牌。这是我应该继续使用还是从头开始?
在此处输入code

 static void Main(string[] args)
{
    string choise = "";
    while (choise != "q" && choise != "Q")
    {

        Console.Clear();
        Console.WriteLine("[1] for a random row of numbers! \n[2] to start the game! 
        \n[Q] to quit! \nPress enter after your selection.");
        choise = Console.ReadLine();
        if (choise == "1")
        {
            RandomNum.randomTal();
        }

  (another class)
  static  Random rnd = new Random();
   public static void Shuffle<T>(T[] array)
    {
        Console.Clear();

        for (int i = 0; i < array.Length; i++)
        {
            int r = i + (int)(rnd.NextDouble() * (array.Length - i));
            T t = array[r];
            array[r] = array[i];
            array[i] = t;
        }
    }

(and another class)

class RandomNum
  {
    public static void randomTal()
    {
        int[] sifferArray = Enumerable.Range(1, 56).ToArray();
        shuffle.Shuffle(sifferArray);
        foreach (var item in sifferArray)
        {
            Console.WriteLine(item);

        }
        Console.WriteLine("Press any key to go back.");
        Console.ReadKey();
    }
static void Main(字符串[]args)
{
字符串choise=“”;
while(choise!=“q”&&choise!=“q”)
{
Console.Clear();
Console.WriteLine(“[1]表示随机数行!\n[2]表示开始游戏!
\n[Q]退出!\n选择后按enter键。“);
choise=Console.ReadLine();
如果(选择=“1”)
{
RandomNum.randomTal();
}
(另一节课)
静态随机rnd=新随机();
公共静态无效洗牌(T[]数组)
{
Console.Clear();
for(int i=0;i
静态void Main(字符串[]args)
{
var bingoCard=getBingoCard();
int colNum=1;
foreach(宾果卡中的变量col)
{
Write(“col”+colNum.ToString()+”);
foreach(列中的var项)
{
控制台。写入(项+“”);
}
Console.WriteLine();
colNum++;
}
Console.WriteLine();
对于(int x=0;x<5;x++)
{
对于(int y=0;y<5;y++)
{
Console.Write(bingoCard[y][x].ToString()+(bingoCard[y][x]>9?”:“”);
}
Console.WriteLine();
}
Console.ReadLine();
}
公共静态int[]getBingoCard()
{
var randGen=新随机数();
var bingoCard=newint[]{
新整数[5],
新整数[5],
新整数[5],
新整数[5],
新国际[5]
};
对于(int y=0;y<5;y++)
{
对于(int x=0;x<5;x++)
{
var-possibleNumber=randGen.Next((15*y)+1,((y+1)*15));
while(bingoCard[y].Any(num=>num==possibleNumber))
{
possibleNumber=randGen.Next((15*y)+1,((y+1)*15));
}
bingoCard[y][x]=可能的枚举数;
}
}
返回宾果;
}

当我需要这样做时,我会使用以下方法:

   private int[] generatePermutation(int size, int seed) 
   {
        var permutation = new int[size];
        Rnd random = new Rnd(seed);

        List<int> permList = new List<int>(size);
        for (int i = 0; i < size; i++) permList.Add(i);

        for (int i = 0; i < size; i++)
        {
            int index = random.Next(0, permList.Count);
            permutation[i] = permList[index];
            permList.RemoveAt(index);
        }
        return permutation;
    }
private int[]generatePermutate(int size,int seed)
{
变量置换=新整数[大小];
Rnd random=新的Rnd(种子);
List permList=新列表(大小);
对于(inti=0;i
如果您有一个类
BingoBoard
,其中嵌入了与单元格对应的2d整数数组,则可以生成如下所示的单元格随机模式:

public class BingoBoard
{
    public const int BoardDimension = 5;

    readonly int[,] board = new int[BoardDimension, BoardDimension];

    public BingoBoard()
    {
        // In column 1 it can only be a number between 1-15 and in column 2 number 16-30 and so on up until column 5 with a number between 61-75.
        for (int iRow = 0; iRow < board.GetLength(0); iRow++)
        {
            var col = Enumerable.Range(iRow * 3 * board.GetLength(1) + 1, 3 * board.GetLength(1)).Shuffle().Take(board.GetLength(1)).ToArray();
            for (int iCol = 0; iCol < board.GetLength(1); iCol++)
                board[iRow, iCol] = col[iCol];
        }
    }

    public int[,] Board
    {
        get
        {
            return board;
        }
    }
}

public static class RandomProvider
{
    // Adapted from http://csharpindepth.com/Articles/Chapter12/Random.aspx
    private static int seed = Environment.TickCount;

    [ThreadStatic]
    static Random random;

    public static Random GetThreadRandom()
    {
        if (random == null)
            random = new Random(Interlocked.Increment(ref seed));
        return random;
    }
}

public class FisherYatesShuffle 
{
    public void ShuffleInPlace<T>(T[] array)
    {
        var randomizer = RandomProvider.GetThreadRandom();

        // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm , second version.
        for (int i = 0; i < array.Length; i++)
        {
            // http://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx
            var j = randomizer.Next(i, array.Length); // i = inclusive lower bound; array.Length = The exclusive upper bound of the random number returned
            array.Swap(i, j);
        }
    }
}

public static class ListExtensions
{
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
    {
        var scrambled = list.ToArray();
        new FisherYatesShuffle().ShuffleInPlace(scrambled);
        return scrambled;
    }

    public static void Swap<T>(this T[] list, int i, int j)
    {
        if (list == null)
            throw new ArgumentNullException();
        if (i != j)
        {
            T temp = list[i];
            list[i] = list[j];
            list[j] = temp;
        }
    }
}
公共类BingoBoard
{
公共const int BoardDimension=5;
只读int[,]板=新int[BoardDimension,BoardDimension];
公共宾戈博德()
{
//在第1列中,它只能是介于1-15和第2列中的数字16-30,依此类推,直到第5列中的数字介于61-75之间。
for(int-iRow=0;iRowpublic class BingoBoard
{
    public const int BoardDimension = 5;

    readonly int[,] board = new int[BoardDimension, BoardDimension];

    public BingoBoard()
    {
        // In column 1 it can only be a number between 1-15 and in column 2 number 16-30 and so on up until column 5 with a number between 61-75.
        for (int iRow = 0; iRow < board.GetLength(0); iRow++)
        {
            var col = Enumerable.Range(iRow * 3 * board.GetLength(1) + 1, 3 * board.GetLength(1)).Shuffle().Take(board.GetLength(1)).ToArray();
            for (int iCol = 0; iCol < board.GetLength(1); iCol++)
                board[iRow, iCol] = col[iCol];
        }
    }

    public int[,] Board
    {
        get
        {
            return board;
        }
    }
}

public static class RandomProvider
{
    // Adapted from http://csharpindepth.com/Articles/Chapter12/Random.aspx
    private static int seed = Environment.TickCount;

    [ThreadStatic]
    static Random random;

    public static Random GetThreadRandom()
    {
        if (random == null)
            random = new Random(Interlocked.Increment(ref seed));
        return random;
    }
}

public class FisherYatesShuffle 
{
    public void ShuffleInPlace<T>(T[] array)
    {
        var randomizer = RandomProvider.GetThreadRandom();

        // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm , second version.
        for (int i = 0; i < array.Length; i++)
        {
            // http://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx
            var j = randomizer.Next(i, array.Length); // i = inclusive lower bound; array.Length = The exclusive upper bound of the random number returned
            array.Swap(i, j);
        }
    }
}

public static class ListExtensions
{
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
    {
        var scrambled = list.ToArray();
        new FisherYatesShuffle().ShuffleInPlace(scrambled);
        return scrambled;
    }

    public static void Swap<T>(this T[] list, int i, int j)
    {
        if (list == null)
            throw new ArgumentNullException();
        if (i != j)
        {
            T temp = list[i];
            list[i] = list[j];
            list[j] = temp;
        }
    }
}