+ Reply to Thread
Results 1 to 3 of 3

Thread: Generating Random Numbers

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    34

    Generating Random Numbers

    Hi , i have that method that generates a random numbers between 1 and 9 :
    Code:
    Random random = new Random();
    for (int i=0;i<100;i++)
    {
        int result = random.Next(1, 9);
        Console.WriteLine(result);
    }
    But it never return 9 , all numbers are between 1 and 8 ONLY , i really couldn`t figure it out , so i though you guys could help me and thanks in advance .

  2. #2
    This means that random.Next(1, 9) will only return values n in the range 1 <= n < 9 , so the result will always in the range of 1 and 8 , to do that correctly , it has to be :
    Code:
    int result = random.Next(1, 10); //<< this will generate randoms between 1 and 9

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    34
    OMG , that`s it ??! , thanks a lot for that .

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts