1

I am trying to generate 4-6 unique numbers in python but when I do uuid.uuuid4(), it generates something like 23dfsFe823FKKS023e343431. Is there a way I can achieve this, generate 4-6 unique numbers like 19391 or 193201.

NB: Beginner with python

glibdud
  • 7,550
  • 4
  • 27
  • 37
  • https://stackoverflow.com/questions/42703059/how-to-create-a-8-digit-unique-id-in-python – Rakesh Sep 09 '19 at 14:04
  • Possible duplicate of [How to create a 8 digit Unique ID in Python?](https://stackoverflow.com/questions/42703059/how-to-create-a-8-digit-unique-id-in-python) – Nikaido Sep 09 '19 at 14:06
  • 3
    If you want a decimal number, you can use `random.randint(x, y)` to generate a random integer between x and y. – John Gordon Sep 09 '19 at 14:06
  • 2
    Outside of context, unique number does not mean much. The natural sequence 1, 2, 3, etc. gives one unique number on each iteration, and provided you store the last number somewhere you will never get twice the same number. If you want *random* numbers, you must say in which interval and which law (gaussian, uniform, etc.). If you want globally unique numbers, you should make them depend on a hardware id (a MAC address for example), and then the time. I am sorry but your question is not precise enough. – Serge Ballesta Sep 09 '19 at 14:11
  • 1
    Please add more detail to your question explaining what you are trying to accomplish and what you mean by unique. – wwii Sep 09 '19 at 14:13

6 Answers6

2

Try this:

import random

nums = set()
while len(nums) < 4: # change 4 to appropriate number
    nums.add(random.randint(0, 1000000))

For example:

>>> nums
set([10928, 906930, 617690, 786206])
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

yes, to make life easy lets use an easy example.

#lets import random, to generate random stuff
import random
#create a result string
result = ''
nums = [1,2,3,4,5,6,7,8,9,0]
for i in range(6):
    result += str(random.choice(nums))

print(result)
NemoMeMeliorEst
  • 513
  • 4
  • 10
2

UUID is for generating Universally Unique Identifiers, which have a particular structure and won't be what you're after.

You can use the random module as follows

import random
id = ''.join(str(random.randint(0,10)) for x in range(6))
print(id)

What does this do?

  • randint generates a random number between 0 inclusive and 10 exclusive, i.e. 0-9
  • calling this with for x in range(6) generates six random digits
  • str converts the digits to strings
  • ''.join forms a single string from the digits
1

You can use random from standard python library

random.randint(a, b)
Return a random integer N such that a <= N <= b.

https://docs.python.org/3/library/random.html#random.randint

In [1]: from random import randint                                                             

In [2]: randint(1_000, 999_999)                                                                
Out[2]: 587848

In [3]: randint(1_000, 999_999)                                                                
Out[3]: 316441
Serhii
  • 1,367
  • 3
  • 13
  • 31
1

To generate N number of a random number or token by specifying a length of Token this will be an easy and feasible solution.

import random

#num[]: by the combination of this, a random number or token will be generated.
nums = [1,2,3,4,5,6,7,8,9,0] 

#all the tokens or random number will be stored in a set called tokens.
#here, we are using a set, because the set will never contain duplicate values.
tokens = set()

result='' #to store temporaray values

while len(tokens) < 10000: # change 10000 to the appropriate number to define the numbers of the random numbers you want to generate.
    #change 6 to appropiate number to defined the length of the random number.  
    for i in range(6):
        result+=str(random.choice(nums))
        tokens.add(result)
        result=''


print(tokens)
#print(len(tokens))
#print(type(tokens))
Akshar Sarvaiya
  • 230
  • 2
  • 8
0

if you want to use uuid to generate number then you can code something like this

digit = 5   # digits in number 

import uuid
for i in range(6):
  print(uuid.uuid4().int[:digit])

Or

from uuid import uuid4
result = [ uuid4().int[:5] for  i in range(6) ]
Somil
  • 1,921
  • 1
  • 21
  • 35