stackauth stackauth
stackauth stackauth

Reputation: 89

How to generate 4-6 unique numbers

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

Upvotes: 1

Views: 3100

Answers (6)

Akshar Sarvaiya
Akshar Sarvaiya

Reputation: 240

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))

Upvotes: 1

Somil
Somil

Reputation: 1941

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) ]

Upvotes: 0

Serhii
Serhii

Reputation: 1587

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

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236124

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])

Upvotes: 2

user3456014
user3456014

Reputation:

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

Upvotes: 2

NemoMeMeliorEst
NemoMeMeliorEst

Reputation: 561

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)

Upvotes: 2

Related Questions