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

7
Contributors
15
Replies
16
Views
8 Years
Discussion Span
Last Post by BestJewSinceJC
Featured Replies
• 2

Instead of just giving you the code, [URL="http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx"]here's[/URL] a link to learn to understand how to use rand and srand. Come back if you have any problems with it.

• 1
Narue 5,707   8 Years Ago

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

• 1
Narue 5,707   8 Years Ago

>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 [B]your[/B] 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. …

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.

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

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:

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.

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.