0

Creating an array of 10 elements and assigning them by counting randomly, assigning a new number if the same numbers are repeated I tried to use the contains method but it didn't appear in the list after the array, I used the exists method but it didn't work either, what kind of way should I follow? thanks

static void Main(string[] args)
{
 Random Rnd = new Random();
 int[] Numbers = new int[10];

 for (int i = 0; i < Numbers.Length; i++)
 {
     int rast = Rnd.Next(10);
     bool b = Array.Exists(Numbers, element => element == rast);
     if (!b)
     {
         i--;
     }
     else { Numbers[i] = rast; }  
  }

  foreach (int item in Numbers)
  {
      Console.WriteLine(item);
  }    
}
John Doe
  • 300
  • 2
  • 11
  • 1
    What you need is https://stackoverflow.com/questions/44549740/randomly-shuffling-an-array (obviously it is not an answer to this question as you are asking why the code does not work rather than how to shuffle an array). – Alexei Levenkov Aug 16 '22 at 23:35
  • Did you mean to do `if(b==true)` – yassinMi Aug 16 '22 at 23:36
  • Are you trying to get an array of N elements, where each element is in the range of `0...N-1`, but the order is random? If so, look up _Shuffle Algorithm_ – Flydog57 Aug 16 '22 at 23:36
  • By the way, there is no need to test `bool` variables for truthiness using `==`. Consider this code: `bool elementExists = Array.Exists(Numbers, element => element == rast); if (!elementExists) {/* some code */}`. It names your condition clearly, and then the `if` tests that condition, again, clearly – Flydog57 Aug 16 '22 at 23:40
  • Avoid repetition of random numbers assigned to a 10-element array – ugur kaymaz Aug 16 '22 at 23:45
  • for (int i = 0; i < Numbers.Length; i++) { int rast = Rnd.Next(10); if (Numbers.Contains(rast)) { i--; } else { Numbers[i] = rast; } contains Method Doesnt work , if it could be work , problem would be solved – ugur kaymaz Aug 16 '22 at 23:47
  • You need to add this `Numbers[i] = rast;` after your `int rast` – Cyrille Con Morales Aug 17 '22 at 00:16
  • put them in a hash set instead – pm100 Aug 17 '22 at 00:19

5 Answers5

1

It appears that you want the numbers from 0 to 9 in an array in random order. If that is so:

var rng = new Random();
var numbers = Enumerable.Range(0, 10).OrderBy(n => rng.NextDouble()).ToArray();
John
  • 3,057
  • 1
  • 4
  • 10
0

Here's an example. I hope this is the output you want.

public static void Main()
{
     Random Rnd = new Random();
     int[] Numbers = new int[10];

     for (int i = 0; i < Numbers.Length; i++)
     {
         int rast = Rnd.Next(10);
         
         bool b = Array.Exists(Numbers, element => element == rast);
         if (!b)
         {
             Numbers[i] = rast;
             Console.WriteLine(rast);
         }
         else {
             i--;
         } 
      }
}
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
0

You're close, but there are two issues in your algorithm:

  1. if (!b) should be if (b)
  2. Rnd.Next(1,10) gets numbers between 1 and 9 so you will never get a 10 and the algorithm will never finish.

After fixing these two your program will work.


Anyway, here's a version that uses .Contains

Random Rnd = new Random();
int[] Numbers = new int[10];

 for (int i = 0; i < Numbers.Length; i++)
 {
     // + 1 because the range of return values includes minValue but not maxValue.
     int r = Rnd.Next(minValue: 1, maxValue: Numbers.Length + 1);
     while(Numbers.Contains(r))
     {
        r = Rnd.Next(minValue: 1, maxValue: Numbers.Length + 1);
     }
     
     Numbers[i] = r;   
  }

Console.WriteLine(string.Join(",", Numbers));
  

Example output:

9,8,7,1,4,6,3,10,5,2
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • i use visual studio 2019 , but unfortunately i receiving this error list Error CS1929 'int[]' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains(ReadOnlySpan, int)' requires a receiver of type 'ReadOnlySpan' – ugur kaymaz Aug 17 '22 at 10:19
  • `using System.Linq;` – tymtam Aug 17 '22 at 23:24
  • Thank you very much, I solved the problem in a different way, I shared it below , its gonna be so useful your method as well – ugur kaymaz Aug 18 '22 at 22:11
0

As a result, I have found the solution, thanks to everyone who answered and showed me the way

        int[] Numbers = new int[10];
        int x, y;
        Random ran = new Random();
        for (x = 0; x < 10; x++)
        {
            Numbers[x] = ran.Next(1, 10);
            for (y = x; y >= 0; y--)
            {
                if (x == y)
                {
                    continue;
                }
                if (Numbers[x] == Numbers[y])
                {
                    Numbers[x] = ran.Next(1, 10);
                    y = x;
                    
                }


            }
            
            Console.WriteLine(Numbers[x]);
        }
       
0
when i created new project as  Console .net in visual studio 2019 ,

it doesnt bring this libraries automatically , Because the following libraries are not attached, the codings I used before did not work, I developed another simple solution, and in this way, I realized that I had a problem due to these libraries not being attached.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

            int[] Numbers= new int[10];

            Random rnd = new Random();
            
            for (int i = 0; i < Numbers.Length; i++)
            {
                int Num= rnd.Next(10);
                if (Numbers.Contains(Num))
                {
                    i--;
                }
                else
                {
                    Console.WriteLine(Numbers[i] = Num);

                }
                


            }