0

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

  • 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. Read More

  • 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. … Read More

2

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.

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

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

0

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?

0

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!

0

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 :(

0

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

0

just these:

#using <mscorlib.dll>

using namespace System;
using namespace std;

i have tried it in visual studio 2003

1

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

Votes + Comments
ROFL :P
0

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

0

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)

1

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

Votes + Comments
Well put.
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.