hi can anyone tell me how to generate random number within certain limit as in between 10 to 20 etc .please help me
Thanks
anuj

no i went thru the link but i needed code just go thru my code

#include<unistd.h>

int main()
{
	int n2,n1;
	int randNum;
	int index;
 int lowest=10, highest=1000; 

	
for(index=0; index<5; index++)
	{ 
    randNum = (int)((highest-lowest)*rand()/(RAND_MAX + 1.0))+lowest; 
	printf("%d\n",randNum);
    }
	return 0;  
}

Is reviving a thread like this one really a good idea?
I don't think so :( the first post is at least six years ago :P

Edit:: I just see that the posts are moved to a new thread, so all what's above in this post doesn't apply anymore :)

no i went thru the link but i needed code just go thru my code

#include<unistd.h>

int main()
{
	int n2,n1;
	int randNum;
	int index;
 int lowest=10, highest=1000; 

	
for(index=0; index<5; index++)
	{ 
    randNum = (int)((highest-lowest)*rand()/(RAND_MAX + 1.0))+lowest; 
	printf("%d\n",randNum);
    }
	return 0;  
}

So whats the problem?

use the following code for random numbers between limits:

Random* r = new Random();

int x=r->Next(10,21);

this will create random numbers between 10 and 20!

use the following code for random numbers between limits:

Random* r = new Random();

int x=r->Next(10,21);

this will create random numbers between 10 and 20!

Doesn't work :(

use the following code for random numbers between limits:

Random* r = new Random();

int x=r->Next(10,21);

this will create random numbers between 10 and 20!

What have you included,
this doesn't look like std c++

just these:

#using <mscorlib.dll>

using namespace System;
using namespace std;

i have tried it in visual studio 2003

>i have tried it in visual studio 2003
From the OP's use of <unistd.h>, which strongly suggests that he's on a POSIX system such as Linux or Unix, I don't think suggesting a C++/CLI solution (a language currently unique to Windows and .NET) is such a hot idea.

Comments
ROFL :P

>i have tried it in visual studio 2003
From the OP's use of <unistd.h>, which strongly suggests that he's on a POSIX system such as Linux or Unix, I don't think suggesting a C++/CLI solution (a language currently unique to Windows and .NET) is such a hot idea.

No!...i am using Windows XP!....i have also tried this code on Visual Studio C++ 2008.and why do you think suggesting a c++/CLI solution aint a good idea?....

No!...i am using Windows XP!....i have also tried this code on Visual Studio C++ 2008.and why do you think suggesting a c++/CLI solution aint a good idea?....

Because the OP probably uses Unix/Linux and they don't support CLI/.NET (yet? :P)

>No!...i am using Windows XP!....i have also tried this code on Visual Studio C++ 2008.
Are you really that dense? It doesn't matter one whit what your setup looks like. If your solution doesn't work on the OP's setup, you've been no help at all. Standard C++ supports random numbers. Are you too good for std::rand?

>and why do you think suggesting a c++/CLI solution aint a good idea?....
Because I think the OP may be using a non-Windows operating system and the only compiler available for C++/CLI is Visual Studio, a Windows program.

Comments
Well put.

I don't see how this is a question of what function you are using. Generate a pseudo-random number, then use the modulus operator to force your result to be between 0 and whatever. Then use addition to bring that result between 10 and 20.

This article has been dead for over six months. Start a new discussion instead.