generating 5 digit numbers

Hi there
Can anyone please tell me how to write a program to generate random 5 digit numbers . thank you

Hi there,

You will need the rand() function and srand() function:
http://www.cplusplus.com/reference/cstdlib/rand/

Let us know if you require any further help.

All the best,
NwN
You want to generate numbers in the range 10000 to 99999.
This is what i came up with : srand (time(NULL));
int number = rand () % 10 000


I have looked at that page it , I want it to also include some numbers that start 0 , let me try and come up with a short program to test it
Last edited on
In that case you will just have to generate a number 5 times:

1
2
3
4
5
6
7
char code[6]; //use a char array to hold the numbers
code[5] = '\0'; //NULL terminate the C-string

srand(time(NULL));

for (int i=0; i<5; ++i)
    code[i] = rand() % 10; //range 0 to 9 


Hope that helps.

All the best,
NwN

int number = rand () % 10000;
This, according to the examples, generates numbers from 0 to 9,999.
int number = 10000 + rand () % 90000;
This, according to the examples, generates numbers ...
int number = rand () % 100000;
This, according to the examples, generates numbers ...

std::setw and std::fill let you print every number (as text) with at least 5 characters and use prefix characters, say '0' (default is ' ') to make it that wide.
To help you better, tell us what you plan on doing with these numbers.
1
2
3
srand (time (0));

int num = (rand() % 1000) + 1;


the possible result is 1 - 1000, do you understand why is it?
I am writing a program to print customer bills , calculate prices , tax , labour cost etc using functions , each customer is identified bt a 5 digit number and that number should appear on the bill and customers with a customer number that start with 'o' may get a discount . So basically I need them to start from 00001 to 99999
If you're using these numbers for identification purposes only, then just use a regular integer. Then, when you need to display them, simply left-pad them with zeroes as necessary. keskiverto suggested the use of std::setw and std::fill, which I support.
Hi there,

Josue Molina wrote:
If you're using these numbers for identification purposes only, then just use a regular integer


I'm not sure I agree. If it's for identification purposes only, it would make sense to just use a string, since the number doesn't need to be calculated with. Furthermore, when you store the id-code in a database, you probably want those zeroes to be there.

Anyway, I'm not an expert, just giving my 2 cents.

All the best,
NwN
I disagree that using a string would be better for two reasons (off of the top of my head):

1. When searching, integer comparison is faster than string comparison. In this case, you would have to perform five times as many comparisons (each index is a five-character array).

2. When inserting, a unique index can be created by increasing the current index by one. If a character array is used, this procedure would become needlessly complicated (casting, carries, overflow, etc.).

3. A third reason could possibly be sorting (it is not guaranteed that a lexicographic comparison will yield desired results).
Last edited on
I'm not too proud to admit when I'm wrong - good points :)

Coming to think of it, in a datastore I would probably use an integer id myself, padding it with zeroes if the display requires it. I guess I was thinking more along the lines of a GUID in the OP's case, which is another thing, of course.

Anyway, thanks for explaining.

NwN
Topic archived. No new replies allowed.