-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;

            }
        }
    }
}
3
  • So Kuuluuko is supposed to randomize them? What have you tried? Also if you compile this what errors do you get? Commented Dec 16, 2011 at 9:18
  • please explain in more details what you want to do, not clear question Commented Dec 16, 2011 at 9:18
  • see stackoverflow.com/questions/254844/… Commented Dec 16, 2011 at 9:24

3 Answers 3

1
var randomNumbers = Enumerable.Range(1,9).OrderBy(n => Guid.NewGuid()).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Looking at the example code, I'm not sure just giving a working answer is going to be much help :)
can you please put the hole method code
@DannyChen: OP is a really newbie in C#, your code is great, but will this help him?
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

Comments

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)];
    }
}

3 Comments

is it? it still doesn't call the Kuuluuko method :)
ah, now it does! however, it still only returns one int from the list, rather than a reordered list.
@Hybrid: Yes, I missed the call to Kuuluuko (what that means in Finnish).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.