is the statement correct to generate a 5 digit number?

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

Where 0+10000 is lowest outcome and 89999+10000 is highest outcome.
Last edited on Nov 20, 2018 at 5:13pm
Nov 20, 2018 at 5:24pm
Can double handle that value.
Nov 21, 2018 at 2:32am
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?
Nov 21, 2018 at 5:20am
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.
Nov 21, 2018 at 6:08am
Can you show us the code?
Nov 21, 2018 at 6:14am
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;
Python;
}}
Nov 21, 2018 at 6:38am
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;
		Python;
	}
	cin.get();
}


time(NULL) returns seconds since 1970 so the seed keeps changing and your program is more random.
Nov 21, 2018 at 6:44am
time(NULL) returns seconds since 1970

(*sigh*)
Nov 21, 2018 at 7:34am
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.Python/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 Nov 21, 2018 at 7:46am
Nov 21, 2018 at 7:53am
> 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.
Nov 21, 2018 at 8:03am
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 Python compilers that don't return time since 1970 but instead some other calender time encoding?


Thanks JLBorges
Nov 21, 2018 at 8:22am
> Are there actually Python 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.
Nov 21, 2018 at 9:04am
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.Python/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.Python/reference/random/uniform_int_distribution/
Nov 21, 2018 at 9:25am
(rand() % (1000 - 100))+ 100)*100 + rand() % 100)

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


Nov 21, 2018 at 5:34pm
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.

Nov 22, 2018 at 2:35am
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.
Nov 22, 2018 at 6:49am
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?
Nov 22, 2018 at 10:30am
Visual Python defines RAND_MAX as 32767.
Pages: 12