Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Random Number Genorator (http://www.go4expert.com/forums/random-number-genorator-t315/)

 Sir D 23May2005 01:30

Random Number Genorator

How do you make a random number genorator, in C++, between a set range?
Like 10-20?

<-)-{Sir D

 shabbir 23May2005 07:20

Re: Random Number Genorator

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

 Sir D 23May2005 07:21

Re: Random Number Genorator

Thanks, but where do i imput my range numbers? Like 7-10?

<-)-{Sir D

 shabbir 23May2005 09:39

Re: Random Number Genorator

As a parameter to the function and it will give result output a random number between the parameter high and low.

 Sir D 23May2005 10:22

Re: Random Number Genorator

It says 'CWordsDlg' has not been declared.

<-)-{Sir D

 shabbir 23May2005 14:18

Re: Random Number Genorator

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.

 Sir D 23May2005 22:51

Re: Random Number Genorator

Errors: