i have this code:
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.
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...
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.
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.
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();
by Enigmativity