is the statement correct to generate a 5 digit number?

Pages: 12
is the statement correct to generate a 5 digit number?
num=rand()%1000+10000;
Yes, but not all five-digit numbers. Only the ones in range 10000-10999.
(Rand() % (100000-10000)) + 10000

Where 0+10000 is lowest outcome and 89999+10000 is highest outcome.
Last edited on
Can double handle that value.
I think you meant to say "Can I double handle that value". If so, what do you mean by that exactly? The answer is probably yes.. what is the exact range you want?
the max number i want is 99999 and the lowest number is 10000.
While running the program it looks like it is stuck in an infinite loop.
One more thing i wanted to ask i want to change random generated after every miilisecond.
Can you show us the code?
this is the code

#include<iostream>
#include<stdlib.h>

using namespace std;

int main()
{double num;
int c=0;
while(c<10)
{
num=(Rand() % (100000-10000)) + 10000;
cout << num << endl;
c++;
}}
You need to seed your rand() using srand() by calling it once in the main function, before rand() is called like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<stdlib.h>
#include<ctime>

using namespace std;

int main()
{
	srand(time(NULL));
	double num;
	int c = 0;
	while (c < 10000)
	{
		num = (rand() % (100000 - 10000)) + 10000;
		cout << num << endl;
		c++;
	}
	cin.get();
}


time(NULL) returns seconds since 1970 so the seed keeps changing and your program is more random.
time(NULL) returns seconds since 1970

(*sigh*)
tpb wrote:
time(NULL) returns seconds since 1970

(*sigh*)


What? Am I wrong? *scratches head*

edit:

The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp).

http://www.cplusplus.com/reference/ctime/time/

Man if you're going to criticize you've got to be more specific. That's the least thing you can do.
Last edited on
> What am I wrong?

In practice, you are right. In every mainstream implementation, the time returned is Unix Time.
https://en.wikipedia.org/wiki/Unix_time

In theory, all that the standard guarantees is that std::time_t is an arithmetic type in which the calendar time is encoded in an unspecified manner.
Okay so the return value could differ from unix time stamp. In which situation would I be working with something like that?

Are there actually C++ compilers that don't return time since 1970 but instead some other calender time encoding?


Thanks JLBorges
> Are there actually C++ compilers that don't return time since 1970 but instead some other calender time encoding?

I do not know of any implementation where it is not Unix time.
num = (rand() % (100000 - 10000)) + 10000;

This isn't guaranteed to generate all 5-digit numbers on a particular system.
RAND_MAX
This macro expands to an integral constant expression whose value is the maximum value returned by the rand function.

This value is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation.

http://www.cplusplus.com/reference/cstdlib/RAND_MAX/

If you are unlucky your statement may only generate numbers up to 42767.

You could try either generating 5 separate digits, or one of the newer random-number distributions:
http://www.cplusplus.com/reference/random/uniform_int_distribution/
(rand() % (1000 - 100))+ 100)*100 + rand() % 100)

Where, (0 + 100)*100 + 0 = 10000 is lowest
And, (899 + 100)*100 + 99 = 99999 is highest


Im getting lost.
rand()%90000; //0 to 89999
now add 10000. range is 10000 to 99999.

answer is just
10000+ rand()%90000; //right?

% is bad about messing up the distribution of random numbers (and most rand() implementations are terrible to boot, sacrificing randomness for speed). Use the modern tools if this bothers you.

answer is just
10000+ rand()%90000; //right?


Yes that's true but RAND_MAX is only guaranteed to be 32767. So there are chances where we are never able to reach the 99999 limit we want. So we generate two numbers below that max and combine them to get the limit we want.
ah. assurances aside, I got
2147483647 for it on mine for rand max. /shrug. Is the OP actually on a severely limited compiler with a low value or are we theorycrafting?
Visual C++ defines RAND_MAX as 32767.
Pages: 12