1

I have to generate random 100 integers from an array belonging to the interval [0 .. 132] and i have to output those which do not belong to the interval (26 .. 100]

I don't know how to output those which do not belong to the interval. I hope someone can give me some guidance. I'm struggling to find information on internet

My first part of the code is:

            Random rand = new Random();
            for (int i=1; i <=100; i++)
            {
                int x = rand.Next(0, 132);
            }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Sevinch
  • 37
  • 7

2 Answers2

2

Instead of generating random numbers which you don't use, generate the ones you need directly:

Random rand = new Random();
for (int i=1; i <=100; i++)
{
    int x = rand.Next(0, 26-0 + 132-101);
    if (x>26) x+=101-26;
}

If the ranges get more complicated, you could use LINQ to construct a concatentation of ranges and take a random from there:

Random rand = new Random(); 
var nums = Enumerable.Range(0,27).Concat(Enumerable.Range(101,32));
for (int i=1; i <=100; i++)     
{
    int x = nums.Skip(rand.Next(0,nums.Count())).First();
}

Don't do that for large ranges. According to @Dmitry Bychenko it can become very slow.

You can also use an if statement as suggested in many comments. But then you need to count the valid numbers and use a while loop instead of a for loop:

Random rand = new Random();
var count = 0;
while (count < 100)
{
    int x = rand.Next(0, 132);
    if (x <= 26 || x > 100)
    {
        count++;
    }
}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
2

Method 1. Using if selection statement

Just check that x value is more than 100 OR value is less than 27:

if (x < 27 || x > 100)
{
    Console.WriteLine(x);
}

Method 2. Using Enumerable.Range LINQ method

Generate range and check that x value does NOT include in specified range:

if (!Enumerable.Range(27, 100).Contains(x))
{
    Console.WriteLine(x);
}

WARNING: Using this method with large range can have a significant impact on increasing the execution time.

Oshawott
  • 539
  • 5
  • 12
  • `!Enumerable.Range(27, 100).Contains(x)` is *dangerous*: when argument(s) is huge, e.g. `!Enumerable.Range(27, 1_000_000_000).Contains(x)` it can be *very slow*. Please, don't recommend such things (or at least add a *warning*, *disclaimer*) – Dmitry Bychenko Jan 03 '23 at 14:05
  • @DmitryBychenko: really? Since [in Python that's incredibly fast](https://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3). I don't see why it can't have a similar implementation in C#. – Thomas Weller Jan 03 '23 at 14:11
  • @Thomas Weller: in c# `Enumerable.Range(start, length).Contains(x)` will enumerate `start`, `start + 1`, `start + 2`, ..., `start + length - 1` items until either `x` is found or all items are enumerated. If `length` is large (and `x` is not found early), the code is slow. – Dmitry Bychenko Jan 03 '23 at 14:15
  • @DmitryBychenko: that's bad. Someone should change the implementation. – Thomas Weller Jan 03 '23 at 14:19
  • @Thomas Weller: please, fiddle yourself: https://dotnetfiddle.net/msR7Ft it required for me ~ **6 seconds** to find out that `1_234_567_890` is out of `[26..1_000_000_26)` range – Dmitry Bychenko Jan 03 '23 at 14:25
  • 1
    You are, probably, looking for `Range` type (left and right borders), not for *enumeration* `Enumerable.Range(...)` which enumerates items within given interval – Dmitry Bychenko Jan 03 '23 at 14:29