0

I want to generate a unique random integer for the variable SEED. This code is part of a bigger script that is going to run multiple times and output each time, so feeding SEED with a non-duplicate random integer is important. Also, I tried random.sample and such that return lists/sequence not an integer, but this is not the case here because of the following line using torch.

SEED = random.randint(1, 1000)
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
2
  • 2
    So what exactly is the problem with the snippet you shared? Commented Jun 25, 2021 at 18:42
  • I need it to generate a duplicate free random integer, but it does not! Commented Jun 25, 2021 at 18:48

3 Answers 3

3

You can also use time.time():

import time
SEED = int(time.time())

This is technically unique since it'll be a different timestamp every time you run the code.

Sign up to request clarification or add additional context in comments.

4 Comments

even if I run it inside a loop that lasts around 3 hours?
I don't know exactly what is your use case, but why don't you initialize SEED outside the loop?
because if I do so it's going to run the loops with the same SEED. I need to run each iteration with a unique SEED.
What if I need to generate multiple unique seeds within 1 second? Your method will fail.
1

Perhaps this is a little too brute-force, but randomly popping integers out of a list of integers seems to work:

min_seed = 1
max_seed = 1000
seed_list = [*range(min_seed, max_seed+1)]

n_repetitions = 300  # or whatever, as long as n_repetitions < len(seed_list)
for repetition in range(0, n_repetitions):
    seed = seed_list.pop(random.randint(min_seed,len(seed_list)))

Since the integers get popped out of the list as they're used as seeds, there won't be repeats.

8 Comments

Lengthy but functional but that's all that matters! Thanks!
Why the list comprehension instead of just list(range(...))?
No real reason, just cranking out code. :^) I prefer the comprehension just because it conveys a bit more intention, at least to me - it reads as "I'm building a list" rather than "I want this other thing to be a list."
For what it's worth, timeit is telling me that list(range(...)) is considerably faster. For 10,000 repetitions of each, the comprehension takes 0.198 sec, and list(range(...)) 0.0855 sec. So there, a counterargument against my original way!
[range(a, b)] creates a list with length 1, but list(range(a, b)) creates a list with length b - 1 - a.
|
1

As an ultimate solution cast UUID to long integer from here:

import uuid
uuid.uuid4().int & (1<<64)-1
9518405196747027403L

3 Comments

how can I edit this to get up to 3 digits?
uuid.uuid4().int & (1<<10)-1
@MahanAghaZahedi while a UUID is guaranteed to be unique, a 3-digit piece of it is not. In fact any manipulation will lose the uniqueness guarantee.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.