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,941 questions

51,879 answers

573 users

How to generate random odd numbers using LINQ in C#

2 Answers

0 votes
using System;
using System.Linq;
  
class GenerateRandomOddNumbers_CSharp {
   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:
   
27
29
31
25
21
33
23
   
*/

 



answered Apr 19, 2024 by avibootz
edited Jul 26, 2024 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic; 

class program {
   static void Main(string[] args) {
        // IEnumerable<int> Range (int start, int count);
         
        IEnumerable<int> oddNumbers = ((ParallelQuery<int>)ParallelEnumerable.Range(20, 17))
                                            .Where(x => x % 2 != 0)
                                            .Select(y => y);
       
        foreach (var n in oddNumbers) {
            Console.WriteLine(n);
        }
   }
}
 
 
  
/*
run:
  
35
21
23
25
27
29
31
33
  
*/

 



answered Apr 19, 2024 by avibootz

Related questions

1 answer 103 views
1 answer 81 views
1 answer 105 views
1 answer 134 views
1 answer 114 views
1 answer 803 views
1 answer 91 views
...