Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to generate random even numbers using LINQ in C#

1 Answer

0 votes
using System;
using System.Linq;

class program {
   static void Main(string[] args) {

        int totalNumbers = 7;

        // IEnumerable<int> Range (int start, int count);
        
        var evenNum = Enumerable.Range(20, 15)
                                .Where(i => i % 2 == 0)
                                .AsParallel() 
                                .OrderBy(i => Guid.NewGuid())
                                .Take(totalNumbers);
      
        foreach (var n in evenNum) {
            Console.WriteLine(n);
        }
   }
}


 
/*
run:
 
30
20
28
32
22
26
24
 
*/

 

 



answered Apr 19, 2024 by avibootz
edited Apr 19, 2024 by avibootz

Related questions

2 answers 151 views
1 answer 105 views
1 answer 134 views
1 answer 107 views
1 answer 103 views
1 answer 803 views
1 answer 91 views
...