生成具有成对数字的数组 [英] Generate array with pairs of numbers
问题描述
我有以下代码:
Random num = new Random();
int check = CheckIfOdd(num.Next(1, 1000000));
int counter = 1;
while (check <= 0)
{
if (check % 2 == 0)
{
check = CheckIfOdd(num.Next(1, 1000000)); ;
}
counter++;
}
int[] nArray = new int[check];
int arLength = 0;
//generate arrays with pairs of numbers, and one number which does not pair.
for (int i = 0; i < check; i++)
{
arLength = nArray.Length;
if (arLength == i + 1)
{
nArray[i] = i + 1;
}
else
{
nArray[i] = i;
nArray[i + 1] = i;
}
i++;
}
确实可以,但是不如我所愿。
which does kinda work, but not as well as i would like.
它应生成一个包含1-1百万个元素的数组,并且其中的数字可以在1-1十亿之间。
It should generate an array with between 1 - 1 million elements, and the numbers within can be between 1 - 1 billion.
它必须在数组中的随机位置(现在不是)对每个数字进行两对处理,然后它应该包含一个没有对的数字...
it has to make two pairs of each number, in random locations in the array ( which it doesn't now ) and then it should contain 1 number which has no pair...
我正在寻找一种更好的方法,因为它不在随机位置,并且不能正确生成1到10亿之间的数字。
I am just looking for a better way of doing it, since it isn't in random locations, and it doesn't generate numbers correctly between 1- 1 billion.
编辑
我被建议这样做:(由oerkelens)
Edit I have been suggested this: (by oerkelens)
var total = new Random().Next(500000) * 2 + 1;
var nArray = new int[total];
for (var i = 1; i < total; i += 2)
{
nArray[i] = i;
nArray[i - 1] = i;
}
nArray[total - 1] = total;
哪个更好,但代码不是很多,但不会将值随机排列。
Which is better, and not as much code, but it doesn't place the values in random order.
编辑2
这几乎可以满足我的需要,但不能生成正确的金额。
如前所述,它最多可以生成x个元素,且数字在1-y之间
Edit 2 This almost does what i need, but it does not generate the right amount. as stated, it should generate up to x elements, with numbers between 1-y
Random r = new Random();
int[] output = Enumerable.Range(0, 11).Select(x => x / 2).OrderBy(x => r.Next()).ToArray();
通过谜语
推荐答案
尝试以下代码:
int[] output = Enumerable.Range(0, 11).Select(x => x / 2).ToArray();
它会产生一个具有以下值的数组:
It produces an array with these values:
{ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5 }
您应该能够将其扩展为所需的任意数量的元素。
You should be able to extend this to as many elements you need.
如果希望随机输出顺序,然后尝试以下操作:
If you want the output in a random order then try this:
Random r = new Random();
int[] output = Enumerable.Range(0, 11).Select(x => x / 2).OrderBy(x => r.Next()).ToArray();
在一个运行中,例如,我得到了这个:
In one run, as an example, I got this:
{ 0, 4, 1, 2, 2, 4, 5, 3, 3, 1, 0 }
要生成具有单个元素的大量随机对,可以执行以下操作:
To produce a large number of random pairs with one single element you can do this:
Random r = new Random();
int pairs = 5; //elements = 2 * pairs + 1;
int max = 100;
int[] output =
Enumerable
.Range(0, pairs)
.Select(x => r.Next(1, max + 1))
.SelectMany(x => new [] { x, x })
.StartWith(r.Next(1, max + 1))
.OrderBy(x => r.Next())
.ToArray();
但是,这不能保证您不会遇到3、4的碰撞
However, this doesn't guarantee that you don't end up with collisions of 3, 4, or more, number clashes.
这不需要 System.Interactive:
This doesn't require "System.Interactive":
int[] output =
new [] { r.Next(1, max + 1) }
.Concat(
Enumerable
.Range(0, pairs)
.Select(x => r.Next(1, max + 1))
.SelectMany(x => new [] { x, x }))
.OrderBy(x => r.Next())
.ToArray();
这篇关于生成具有成对数字的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!