CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Problem on Generating Random Numbers......

    Hi!
    I am stuck in a problem which requires generation of 7 integer random numbers between 0 to 12, and store it in the 7 different variables.....what i want to do is all those 7 variable have distinct or unique values or the random numbers assigned to the variables must not repeat........

    Secondly, I have to store a 3 digit random number between -999 to 999... now i dont want numbers between -99 to 99 so please suggest me alternative method if there exist....
    Code:
       
       do
        {
                   a=aRan.Next(-999,999);
         }while(a>=-99 && a<=99);

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Problem on Generating Random Numbers......

    Quote Originally Posted by rocky_upadhaya View Post
    Hi!
    I am stuck in a problem which requires generation of 7 integer random numbers between 0 to 12, and store it in the 7 different variables.....what i want to do is all those 7 variable have distinct or unique values or the random numbers assigned to the variables must not repeat........

    Secondly, I have to store a 3 digit random number between -999 to 999... now i dont want numbers between -99 to 99 so please suggest me alternative method if there exist....
    Code:
       
       do
        {
                   a=aRan.Next(-999,999);
         }while(a>=-99 && a<=99);
    The code you posted seems like it should work. You should note, however, that while the first parameter of the Next method is inclusive, the second one i exclusive, meaning that 999 would never be returned in your current code.

    I put this simple app together to illustrate how I'd do it. I would recommend storing the variables in a collection in stead of defining them all though, as it makes for much prettier code

    Code:
    using System;
    using System.Collections.Generic;
    
    namespace QuickSharp
    {
        class Program
        {
            private static Random aRan = new Random(DateTime.Now.Millisecond);
            private static List<int> returnedvalues = new List<int>();
            static void Main()
            {
                int val1 = GetNum(1,13, false);
                returnedvalues.Add(val1);
                int val2 = GetNum(1,13, false);
                returnedvalues.Add(val2);
                int val3 = GetNum(1,13, false);
                returnedvalues.Add(val3);
                int val4 = GetNum(1,13, false);
                returnedvalues.Add(val4);
                int val5 = GetNum(1,13, false);
                returnedvalues.Add(val5);
                int val6 = GetNum(1,13, false);
                returnedvalues.Add(val6);
                int val7 = GetNum(1,13, false);
                returnedvalues.Add(val7);
                
                int val8 = GetNum(-999,1000, true);
                int val9 = GetNum(-999, 1000, true);
                int val10 = GetNum(-999,1000, true);
                Console.WriteLine(val1);
                Console.WriteLine(val2);
                Console.WriteLine(val3);
                Console.WriteLine(val4);
                Console.WriteLine(val5);
                Console.WriteLine(val6);
                Console.WriteLine(val7);
                Console.WriteLine(val8);
                Console.WriteLine(val9);
                Console.WriteLine(val10);
                Console.ReadKey(true);
            }
            
            private static int GetNum(int start, int end, bool filter)
            {
                int a = 0;
                do
                {
                    a=aRan.Next(start,end);
                }while(returnedvalues.Contains(a) || (filter && a<100 && a>-100));
                return a;
            }
        }
    }
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Re: Problem on Generating Random Numbers......

    Thank You for your Help and suggestion, method worked perfectly on me..

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured