Started learning Python yesterday; I know little more than what is here, below.
The random generator functions but the same number applies to each entry.
Could someone show me how to generate a unique number for each entry (one, two, three)?
Thank you.
Code so far:
import random
number_gen = random.choice(range(100))
num = str(number_gen)
for group in ("Group_A:", "Group_B:"):
print(group)
print(" one", num)
print(" two", num)
print(" three", num)
print()
Calling means using the () at the end, like when you called str in num = str(number_gen). You can create your own function that creates a random number, then call it whenever you need one.
>>> from random import randint
>>> help(randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
# this just saves having to use choice(range())
>>> def num():
... return randint(1,10)
...
>>> for x in ('a', 'b', 'c'):
... print(x, num())
...
a 3
b 2
c 5
It really depends on what you need but the random package is not a good goto. Thats for random generation, not unique number generation.
import uuid
uuid.uuid4().int
this will give you a unique integer each time it is run. This number is universally unique so in theory its unique to your machine at the time of execution so the chance of collision is infinitesimally small
That being said this number is going to be VERY big. It really depends on what your uniqueness needs are. If you simply need a number which is unique per session you can have an incrementing counter
No, YOU be more specific about how uuid4 is better than random. You necro’d a thread to tell people to avoid the random module. Without good justification, that’s FUD and has no value to anyone.
The reason to use uuid over random is that it handles correctly seeding the generator and ensuring that you have a wide enough keyspace to provide a sufficient level of randomness that in the majority of practical use cases you should never experience a collision.
I asked you what level of uniqueness you were referring to is I wanted to understand your position, you made a very dogmatic assertion and I wanted to make sure I wasn’t missing something. I’m really not sure why your bringing out the guns here and throwing about insults, If I’m wrong here correct me with something specific, that way atleast there is benifit.
More baseless FUD. The random module seeds the generator correctly, and uses a Mersenne twister with a period of 2**19937-1 which is a lot more than you’ll need for most purposes.
YOU started with the dogmatic assertion that the random module “is not a good goto”. You started out by making a bold claim with no basis.
Probably what you’re missing is that Python’s random module is a lot better than you’re giving it credit for?
You are spreading FUD and I’m calling it like it is. If you repeat what you were told without checking it, then you may not have invented it, but it’s still causing the same problems. Remember, Python doesn’t ship bad modules just to mess with people. Think before you post - why would Python include a bad random module?
Probably just random.randrange(some_big_number) if I want randomly generated IDs. They’ll usually be good enough, and certainly they’ll be no worse than UUID4 if you pick a number that’s 2**128 or higher. But if I actually care about uniqueness, I have a central broker - usually a database - that checks them. Otherwise, anything that generates randomly is about as good as anything else that generates randomly, with the chance of collisions, so I would just use the simplest and most obvious: the random module.