The Student Room Group

How to create a short sequence with random numbers in python?

How do you generate a short sequence of random numbers and allocate it to the user on python. Like say if they input the question 'How do i fix my computer?' , they should be given a unique id and they will basically be given a case number which will then be sent off to a technician.



How would I code this with python??

Would anyone be able to give an example please?????

Thanks
Reply 1
bump
If you want a case number that is (somewhat) easy to remember or communicate verbally then you should look at the random module, however depending on how large a number you generate and how long you will be generating numbers for it would be possible to eventually get a conflict of duplicate numbers (so you would have to keep track of previous numbers and if you get one again regenerate), if you don't care about readability and don't want to keep track of previous id's then using the uuid module would be better and drastically lower the probability of getting a duplicate id.>>> import uuid
>>> str(uuid.uuid4())
'c5227b41-8331-46be-935f-5acdb9662b57'
>>> str(uuid.uuid4())
'213b0409-c051-4f04-a3ac-af8be4d7977e'
>>> str(uuid.uuid4())
'a1fbeb4f-f901-425d-a15a-5f65c64e7b0a'
>>> str(uuid.uuid4())
'7e23039e-7035-4afc-adf1-7a995da7bdf7'
>>> str(uuid.uuid4())
'744ba11e-d379-4368-84bb-c8ab2eebdb8b'
>>> str(uuid.uuid4())
'b5e888d9-2fe4-4172-bcc6-e2a1d656161a'
>>> str(uuid.uuid4())
'a410e726-3b5b-48a1-a44a-df38f6cafdef'
>>> str(uuid.uuid4())
'175d5669-3480-4c67-8113-95cd2f9ff393'
>>> str(uuid.uuid4())
'078b9e59-74e6-402b-b7f1-5b2ace959bfd'
>>> str(uuid.uuid4())
'ab43b628-65be-4c51-b6c0-afc7141d84c8'
(edited 7 years ago)
Original post by bluepearl7
How do you generate a short sequence of random numbers and allocate it to the user on python. Like say if they input the question 'How do i fix my computer?' , they should be given a unique id and they will basically be given a case number which will then be sent off to a technician.



How would I code this with python??

Would anyone be able to give an example please?????

Thanks


With the use of variables and random.randint.
Assign the list of random numbers to a new variable.

http://stackoverflow.com/questions/22842289/python-generate-n-unique-random-numbers-within-a-range
Reply 4
from random import randint #imports relevant libraries

caseNumber = (randint(1,100)) * 12345#defines caseNumber to equal a random number, multiplies that number by 12345 so it is a long number
print (caseNumber)

That will work, but it's definitely not the best way to do it
Reply 5
Very nice and simple way to get a 5 figure number everytimefrom random import randint

total = ''
for i in range (5):
x = randint(1,9)
total = total + ''.join(str(x))
print (total)
(edited 7 years ago)

Quick Reply

Latest

Trending

Trending