andrelange91 - 6 months ago 47

C# Question

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.

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.

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();

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Try this code:

```
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();
```

However, this doesn't guarantee that you don't end up with collisions of 3, 4, or more, number clashes.

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();
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**