Random Number Genorator

Discussion in 'C' started by Sir D, May 22, 2005.

  1. Sir D

    Sir D New Member

    Joined:
    May 22, 2005
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    How do you make a random number genorator, in C++, between a set range?
    Like 10-20?

    <-)-{Sir D
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hello

    First of all I would like to Welcome you to this community of programmers.

    Here is the code to do what you need
    Code:
    int CWordsDlg::GetRandomNumer(int high,int low)
     {
       time_t SeedTime;
       struct tm SeedDate;
       SeedTime = time(0);
       SeedDate = *localtime(&SeedTime);
       int FinalSeed = (int)SeedTime + SeedDate.tm_mday + (SeedDate.tm_mon+1) + (SeedDate.tm_year+1900);
       srand((unsigned int) FinalSeed);
     
       int Interval = high - low + 1;
       int RandomOffset = rand() % Interval;
       int RandomNumber = low + RandomOffset;
       return RandomNumber;
     }
     
        
    Its a fairly simple function which needs any explanation but then also if you need any just put a post here.

    Thanks
    Shabbir Bhimani
     
  3. Sir D

    Sir D New Member

    Joined:
    May 22, 2005
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks, but where do i imput my range numbers? Like 7-10?

    <-)-{Sir D
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As a parameter to the function and it will give result output a random number between the parameter high and low.
     
  5. Sir D

    Sir D New Member

    Joined:
    May 22, 2005
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    It says 'CWordsDlg' has not been declared.

    <-)-{Sir D
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Just remove that
    Code:
    int GetRandomNumer(int high,int low)
     {
       time_t SeedTime;
       struct tm SeedDate;
       SeedTime = time(0);
       SeedDate = *localtime(&SeedTime);
       int FinalSeed = (int)SeedTime + SeedDate.tm_mday + (SeedDate.tm_mon+1) + (SeedDate.tm_year+1900);
       srand((unsigned int) FinalSeed);
     
       int Interval = high - low + 1;
       int RandomOffset = rand() % Interval;
       int RandomNumber = low + RandomOffset;
       return RandomNumber;
     }
    
    As it was taken from my MFC Application that class name came up.
     
  7. Sir D

    Sir D New Member

    Joined:
    May 22, 2005
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Errors:
    [Linker error]uderline reference to 'WinMain@16'
    Id returned 1 exit status

    <-)-{Sir D
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Linker error are because there are some errors in your Main Program and not in the code given above.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice