如何生成一定数量的数字并将其随机分布在网格上? [英] How to generate a certain amount of numbers and spread them randomly across a grid?
问题描述
我要生成5次 2 ,并生成10次 1 .我试图将它们随机分布在Delphi中的String Grid上.我还想用 0 填充网格的其余部分,而不是 1 或 2 .我什至不知道如何从这里开始.
I want to generate the number 2 5 times and the number 1 10 times. I'm trying to spread these across a String Grid in Delphi randomly. I also want to fill the rest of the grid that isn't 1 or 2, with 0's. I have no idea how to even start here.
看起来像这样(P代表玩家,只有5 2和10 1): https://gyazo.com/aeef05c3a92ce7847c0f42ad40faa733
It would look something like this (P stands for player and there would only be 5 2's and 10 1's): https://gyazo.com/aeef05c3a92ce7847c0f42ad40faa733
推荐答案
给出一个尺寸为 m × n 的网格,创建一个长度为 m的数组* n
.将五个2和10 1放入数组中,并用0填充其余部分.(我们假设 m 和 n 的乘积至少为15.)随机排列数组.将改组后的数组的每个元素复制到网格中的连续单元格中.
Given a grid with dimensions m×n, create an array of length m * n
. Put five 2's and 10 1's in the array, and fill the remainder with 0's. (We'll assume the product of m and n is at least 15.) Shuffle the array. Copy each element of the shuffled array into successive cells in the grid.
这篇关于如何生成一定数量的数字并将其随机分布在网格上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!