# Generate a unique number for each entry

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

This means, choose a number right now, and remember it. It does not mean “every time we use this variable, do the calculation again”.

If you need more random numbers, then you need to call it more times. For example, by calling it at the point where you need it.

Incidentally, you should learn the basic techniques for string formatting:

Thank you, it’s all confusing to me, but it’ll get easier the more I do, of course.

I did call it at the points I needed it.

Note that just calling `random.choice` multiple times does not ensure that you will get different values each time.

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

Well that’s completely obvious by the fact I stated that’s the problem, in my OP - I already knew that.

A function from the `random` module that is designed to produce random sample without replacement is `random.sample`.

So, you could do

``````values = random.sample(range(100), k=6)

for i, group in enumerate("Group_A:", "Group_B:"):
print(group)
print("  one", values[i + 0])
print("  two", values[i + 1])
print("  three", values[i + 1])
print()
``````

That certainly provides the result. Now I have to work through it to understand it.

Thank you, much appreciated.

And, for the record, this is for personal use, not for school; I’m nearly 60, not schoolage - just in case anyone’s wondering.

Thanks, again.

I’m sure you mean well, but that is gibberish to me - again, I started learning Python yesterday; I have no idea what that all means.

Thanks, but we can end the conversation here before there’s more confusing information.