-1

I've method which put the numbers 1 to 9 in a random order. Each number appears only once. The method should return an integer table.

This is my code which facing the wall, I ran out of ideas with this code. I know that this code is 100% wrong.

    class Program
    {
        static void Main(string[] args)
        {
            int luvut = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine(luvut);
            Console.ReadKey();
        }

        private static int Kuuluuko(int luvut)
        {
            for (int i = 0; i < luvut.Length; i++)
            {

                return;

            }
        }
    }
}
sikender
  • 5,883
  • 7
  • 42
  • 80
coodienoobie
  • 129
  • 5
  • 13

3 Answers3

1
var randomNumbers = Enumerable.Range(1,9).OrderBy(n => Guid.NewGuid()).ToList();
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
0

What about something like this? Make a list, and remove the numbers from that list as you use them up. Like drawing numbers from a bowl or something like that :)

var numbers = new List<int>(new[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9});
var randomnumbers = new List<int>();
var rnd = new Random();
while( numbers.Count > 0 ){
  var index = rnd.Next(0, numbers.Count);
  randomnumbers.Add(numbers[index]);
  numbers.RemoveAt(index);
}
//randomnumbers now contain the random sequence
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
0

Your code has a couple of errors and didn't even compile. Here is the code running and giving you what you expect:

class Program
{
    static void Main(string[] args)
    {
        int[] luvut = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Console.WriteLine(Kuuluuko(luvut));
        Console.ReadKey();
    }
    private static int Kuuluuko(int[] luvut)
    {
        var random = new Random();
        return luvut[random.Next(0, luvut.Length)];
    }
}
Fischermaen
  • 12,238
  • 2
  • 39
  • 56