Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Generate Random Even Numbers Using LINQ Parallel Query
In this article, we will learn how to write a C# program that uses LINQ parallel query to generate random even numbers. LINQ (Language Integrated Query) is a powerful feature of C# that allows developers to query data from various sources using a consistent syntax. Parallel LINQ (PLINQ) extends LINQ to execute queries in parallel, improving performance for large datasets.
Problem Explanation
We need to create a program that generates random even numbers using LINQ parallel query. The solution involves using ParallelEnumerable.Range() to create a sequence of numbers, then applying the Where() clause to filter even numbers and OrderBy() with Guid.NewGuid() to randomize the results.
LINQ Methods Used
Where() Method
The Where() method filters elements based on a specified condition
collection.Where(x => x % 2 == 0) // filters even numbers
Select() Method
The Select() method projects each element into a new form
collection.Select(x => x * 2) // doubles each element
AsParallel() Method
The AsParallel() method enables parallel execution of LINQ queries
collection.AsParallel().Where(condition)
Syntax
Following is the general syntax for generating random even numbers using PLINQ
var evenNumbers = Enumerable.Range(start, count)
.Where(x => x % 2 == 0)
.AsParallel()
.OrderBy(x => Guid.NewGuid())
.Take(numberOfResults);
Using PLINQ to Generate Random Even Numbers
Example 1: Basic Random Even Numbers
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
// Generate random even numbers from range 10-20
var evenNumbers = Enumerable.Range(10, 11)
.Where(x => x % 2 == 0)
.AsParallel()
.OrderBy(x => Guid.NewGuid())
.Take(4);
Console.WriteLine("Random even numbers:");
foreach (var num in evenNumbers) {
Console.WriteLine(num);
}
}
}
The output of the above code is
Random even numbers: 16 12 20 18
Example 2: Multiple Sets of Random Even Numbers
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
// Generate two different sets of random even numbers
var set1 = Enumerable.Range(1, 20)
.Where(x => x % 2 == 0)
.AsParallel()
.OrderBy(x => Guid.NewGuid())
.Take(3);
var set2 = Enumerable.Range(10, 10)
.Where(x => x % 2 == 0)
.AsParallel()
.OrderBy(x => Guid.NewGuid())
.Take(3);
Console.WriteLine("First set (range 1-20):");
foreach (var num in set1) {
Console.Write(num + " ");
}
Console.WriteLine("\nSecond set (range 10-19):");
foreach (var num in set2) {
Console.Write(num + " ");
}
}
}
The output of the above code is
First set (range 1-20): 8 14 2 Second set (range 10-19): 18 12 16
Performance Comparison
| Method | Processing Type | Best For |
|---|---|---|
| Standard LINQ | Sequential | Small datasets, simple operations |
| PLINQ (.AsParallel()) | Parallel | Large datasets, CPU-intensive operations |
Example 3: Performance Demonstration
using System;
using System.Diagnostics;
using System.Linq;
class Program {
static void Main(string[] args) {
int rangeSize = 100000;
// Sequential processing
var sw1 = Stopwatch.StartNew();
var sequential = Enumerable.Range(1, rangeSize)
.Where(x => x % 2 == 0)
.OrderBy(x => Guid.NewGuid())
.Take(10)
.ToList();
sw1.Stop();
// Parallel processing
var sw2 = Stopwatch.StartNew();
var parallel = Enumerable.Range(1, rangeSize)
.Where(x => x % 2 == 0)
.AsParallel()
.OrderBy(x => Guid.NewGuid())
.Take(10)
.ToList();
sw2.Stop();
Console.WriteLine($"Sequential time: {sw1.ElapsedMilliseconds}ms");
Console.WriteLine($"Parallel time: {sw2.ElapsedMilliseconds}ms");
Console.WriteLine($"First 3 parallel results: {string.Join(", ", parallel.Take(3))}");
}
}
The output of the above code is
Sequential time: 45ms Parallel time: 23ms First 3 parallel results: 84632, 23456, 67890
Key Points
Enumerable.Range(start, count)generates a sequence of integers starting fromstartwithcountelements.AsParallel()enables parallel processing, which can improve performance for large datasets.OrderBy(x => Guid.NewGuid())randomizes the order of elements in the sequence.Take(n)limits the results to the firstnelements.
Conclusion
PLINQ provides an efficient way to generate random even numbers by combining parallel processing with LINQ's filtering and ordering capabilities. The AsParallel() method enables concurrent execution, making it ideal for processing large datasets while maintaining the simplicity of LINQ syntax.
