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

Instead of just giving you the code, here's a link to learn to understand how to use rand and srand. Come back if you have any problems with it.

commented: Hmm, where do I know that site from? :P +5
commented: Gotta ++ the site plug. +19

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;  
}

You didn't 'seed' the random generator with srand(), so your randomnumbers will not be so random :)

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?

To the OP:
This page explains briefly what you want to do.

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.

commented: ROFL :P +6

>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.

commented: Well put. +32

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.