Generate a unique number for each entry

Awesome. Thank You for correcting me.

Perhaps we have a different defination of the word, to me FUD is spreading misinformation to cause harm or manipulate people. I’m happy to admit I was wrong and the material effect is similar but I had no intention of causing harm. Throwing insults about just makes things toxic.

I’m genuinely sorry for wasting your time, but it seems unreasonable to ascribe malice to my ignorance :wink:

thats really neat, thankyou.

If you want to call it ignorance rather than malice, then the responsibility is on you to check your facts befoer you make bald assertions. Telling people that the random module is “not a good goto” is exactly FUD. Very very few people actually invent false information with malicious intent; huge numbers of people spread it.

@admins could you please go ahead and remove my account. I’m thinking I’m not a good fit here

Please stay! Your posts here were reasonable to my eyes. @Rosuav was overly grumpy :frowning:.

I really don’t have the energy Tim. I joined to add to a conversation where I thought I could offer some insight, looking back my tone and delivery were definitely quite flawed. I really appreciate having been corrected, I just don’t get why I’m being accused of being a malicious spreader of deliberately misleading information that wasn’t my intention though I guess I can understand how having to deal with posts like mine on the day to day can frustrate a person.

regardless I think this forum really isn’t for me so please go ahead with the closure.

Nah, no need to leave so soon.

When a new user arrives on the scene and posts for the first time, other users like myself see a message reminding us to welcome the new user. For example, your first post has this message above it:

This is the first time Rob has posted — let’s welcome them to our community!

So, welcome to the Python community, Rob!

As in my case upon arrival a few years ago, you may see some messages from the discobot, inviting you to respond in order to learn more about this forum. When done, you’ll even get a certificate to hang on your wall. :grin: There’s also an advanced tutorial. So, please stick around, find out what discobot has to show you, see what good stuff is going on around here in the various categories, and have a good time!

You were accused of being a spreader of misleading information. I never said malicious, because it actually doesn’t make any difference.

Except you used the acronym FUD, which, as the definition @rfletchr quoted, is generally understood to mean deliberate deception. Which wasn’t the case here. An emotionally loaded characterization of a technical misunderstanding was simply inappropriate, and it’s not hard at all to understand why @rfletchr took offense.

For the technical content, you’re right: there’s every reason to believe randrange(2**128) in Python is less likely to yield a duplicate than uuid4(). It’s certain that either way will eventually deliver duplicates. And there are much simpler ways to guarantee uniqueness that don’t involve randomness at all.

@rfletchr added value to the last point by posting a simple function to do so, and I added by giving a one-liner way that’s also thread-safe.

Your behavior got the newcomer to ask admins to delete their account and yet you double-down your unnecessarily abrasive tone. Stop.

5 Likes

Nitpick:

1.39 μs  randrange(2**128)
0.12 μs  getrandbits(128)
Code

Python, 218 bytes

from timeit import repeat

for e in [
    'randrange(2**128)', 
    'getrandbits(128)',
] * 3:
    t = min(repeat(e, 'from random import randrange, getrandbits', number=10**5)) / 10**5
    print(f'{t*1e6:.2f} μs ', e)

Attempt This Online!

If you need random numbers but never want duplicates, even across multiple program runs, you can use the ‘unique_random’ package.

unique_random: https://pypi.org/project/unique-random/

Install the package: pip install unique-random

Example: Generate unique random integers

from unique_random import UniqueRandom

# Set the range
start = 1
end = 100

# Create a generator that remembers used numbers
ur = UniqueRandom(start, end, persistent=True)

# Generate 10 unique random numbers
for _ in range(10):
    print(ur.randint())

# Always close after use
ur.close()
output:
27
53
14
72
6
33
89
91
15
47

Why this package is useful

  • It never repeats a number.

  • It works across multiple runs of your script, until you reset.

  • It supports very large ranges (millions or even billions).

Reset the state

If you ever want to start over:

unirand --reset --start 1 --end 100

or you can reset directly before closing the program

# Reset
print(“Resetting state file…”)
ur.reset()

# Close the file
ur.close()