F
F
Fithanso2021-09-06 20:38:48
Python
Fithanso, 2021-09-06 20:38:48

How to generate an ID (random number) that is less than the given one, but has the same number of digits in the record?

Greetings. The task is as follows: there is a maximum ID value. It is necessary that the new id be smaller, but always have a certain number of digits in the record. Either 1 less or 2 less. Example of maximum id: 123434131 I understand that the task is not difficult, but I can't come up with a solution that would seem beautiful. Please share your options. Desirable implementation in python. ? Maybe a library function? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fithanso, 2021-09-06
@Fithanso

thanks to those who commented and replied. you can use randrange up to max. ID, and if the number is less, fill with zeros from the beginning

R
Rsa97, 2021-09-06
@Rsa97

ID 0 = MAX_ID
ID n = ID n-1 - 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question