+ 1

How can I generate a unique 4 digit id for each human?

I've been trying all morning but nothing works. https://code.sololearn.com/c454ZOctiB5J/?ref=app

3rd Mar 2019, 6:24 PM
Daniel Cooper
Daniel Cooper - avatar
7 ответов
+ 3
I added some C to generate a unique random four digit id for each person (set DEBUG to 0 for a four digit id). I'm sure there's a more elegant way in C++ though 👀 https://code.sololearn.com/c14fG6yeMah9/?ref=app
3rd Mar 2019, 9:33 PM
Anna
Anna - avatar
+ 1
Seems to work to me. You get unique ids from 1 already so why not change it to static int id = 1000; // error handling when id == 10000 ? instead if you want 4 digits. If you want 4 digits starting from 0 or 1 you can use the same setw thingy I explained earlier to get 0000, 0001, 0002 etc.
3rd Mar 2019, 7:19 PM
Dennis
Dennis - avatar
+ 1
I want the object count + the random 4 digit id for each human Everything I've tried either results in zero, four zeros, four of the same number, or a random number in general
3rd Mar 2019, 9:10 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
https://code.sololearn.com/cgwX2ZC6WIEo/?ref=app I found a way to generate a 4 digit number but it won't work the same in a class and I don't know why I suppose you could also do something along the lines of multiplying a number by 10 and adding a random digit For example, 23*10+3=233 and make the random digit a random number, but I haven't really tried that yet.
3rd Mar 2019, 10:30 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
If you can get it to work I'll be impressed lol
3rd Mar 2019, 11:22 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Almost forgot to post the working code lol https://code.sololearn.com/c3e5bMa0NvlY/?ref=app This is the one that I can't get to work with my code
3rd Mar 2019, 11:25 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Here's a quick implementation I made that generates a unique number between min and max (inclusive). If a number is generated that has already been generated before it simply selects the next available number and it wraps around, also supports removal in case a human dies. https://code.sololearn.com/cmz043bu85MW/?ref=app
4th Mar 2019, 12:08 AM
Dennis
Dennis - avatar