# Using LINQ and Random Numbers

Page 1 of 1

## 5 Replies - 5744 Views - Last Post: 15 December 2011 - 09:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=259891&amp;s=8c25f9ea8db032333ae54593d3a75c7d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JohnYossarian

Reputation: 0
• Posts: 1
• Joined: 14-December 11

# Using LINQ and Random Numbers

Posted 14 December 2011 - 03:01 PM

I need to generate a 30 random number array and use linq to sort it . I have the following code so far, but it is not doing any of the sorting. Just shows the numbers. Any ideas?

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sort_Arrays
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
var randomNumbers = Enumerable.Range(1, 30)
.Select(x => new { val = x, order = rnd.Next() })
.OrderBy(i => i.order)
.Select(x => x.val)
.ToArray();

Display (values, "Random Numbers:");

var sortAscending =
from value in randomNumbers
orderby value
select value;

Display(sortAscending,"Sorted(Ascending:");

var sortDescending =
from value in randomNumbers
orderby value descending
select value;

Display(filtered, "Sorted(Descending:");

}

public static void Display( IEnumerable<int> results, string header)

{

foreach (var element in results)
Console.Write("{0}", element);

Console.WriteLine();

}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Using LINQ and Random Numbers

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Using LINQ and Random Numbers

Posted 14 December 2011 - 03:13 PM

You don't seem to actually do a sort on your data.

http://msdn.microsof...array.sort.aspx

### #3 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• Joined: 08-June 10

## Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 08:49 AM

I think it's because your code is a bit confused.

First, you're selecting the random number into "Order", and the index number into "Val." To me, that doesn't make any sense.

So, rewriting that part:

```var numbers = Enumerable.Range(1, 30).Select(x => new {Value = rnd.Next(), Index = x}).ToArray();
```

Now we have an array of a generic type that contains an index number, and a random value.

Next, to sort ascending by the random number.

```var asc = numbers.Select(x => x.Value).OrderBy(x => x);
```

Then, the same thing sorted by descending:

```var desc = numbers.Select(x => x.Value).OrderByDescending(x => x);
```

Much simpler, no?

Btw, tlhIn`toq, you can use LINQ to sort (OrderBy and OrderByDescending, ThenBy and ThenByDescending), and it's somewhat simpler than Array.Sort, since you can define the comparison to sort by with a lambda, rather than an IComparer.

### #4 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:03 AM

I really am far behind the LINQ curve. So many cool software technologies, so little time.

### #5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5104
• Posts: 9,283
• Joined: 08-June 10

## Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:14 AM

LINQ is awesome. It's like...SQL for collections in memory, but better. Seriously, check it out. You have to learn to love lambdas, but once you understand them, you'll find more uses for them then you thought you could.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Using LINQ and Random Numbers

Posted 15 December 2011 - 09:28 AM

Curtis Rutland is talking about LINQ To Objects. Most of the "functionality" comes for the QueryProviders.

LINQ operates over anything that matches the correct pattern, its doesn't need to implement IEnumerable or IQueryable.

I think the best implementer of QueryProviders are from Bart De Smet

This post has been edited by AdamSpeight2008: 15 December 2011 - 09:36 AM