convert a list to a string

B

Bart Nessux

number = random.sample(range(count), 1)

This makes 'number' into a one entry list (I don't know why as 'count'
is an integer). Is there an easy way to convert 'number' back to an int?

TIA
 
P

Paul Rubin

Bart Nessux said:
number = random.sample(range(count), 1)

This makes 'number' into a one entry list (I don't know why as 'count'
is an integer). Is there an easy way to convert 'number' back to an
int?

number = number[0]

But why are you using random.sample for that? And making a list of
size count, just to throw it away? It sounds like you just want a
random number between zero and count. Do that with:

number = random.randint(count)
 
B

Bart Nessux

Paul said:
Bart Nessux said:
number = random.sample(range(count), 1)

This makes 'number' into a one entry list (I don't know why as 'count'
is an integer). Is there an easy way to convert 'number' back to an
int?


number = number[0]

But why are you using random.sample for that? And making a list of
size count, just to throw it away? It sounds like you just want a
random number between zero and count. Do that with:

number = random.randint(count)

How does randint and sample differ? I was under the impression that
sample was more random.
 
B

Bart Nessux

Paul said:
Bart Nessux said:
number = random.sample(range(count), 1)

This makes 'number' into a one entry list (I don't know why as 'count'
is an integer). Is there an easy way to convert 'number' back to an
int?


number = number[0]

But why are you using random.sample for that? And making a list of
size count, just to throw it away? It sounds like you just want a
random number between zero and count. Do that with:

number = random.randint(count)

Forgot to say thanks! the number[0] thing is great!
 
P

Paul Rubin

Bart Nessux said:
How does randint and sample differ? I was under the impression that
sample was more random.

No, they both use the same underlying generator.

randint gives you one random number, which is what you want.
sample takes a list and selects some elements from it.

As I mentioned in another post, the underlying generator is designed
to have good statistical properties for doing things like simulations.
It's not designed to withstand someone actively trying to predict the
next random number based on the previous output stream. If you're using
it in an application like a high-stakes online game, where players have
significant incentive to predict the output, then you should get your
random numbers a different way. Doing that kind of thing properly
takes a lot of skill though--it's not something newbies should attempt.
 
J

Jeff Epler

How does randint and sample differ? I was under the impression that
sample was more random.

sample is useful when you want to choose several items from the
population. The degenerate case
random.sample(range(100), 1)
is equivalent to
[random.choice(range(100))]
is equivalent to
[random.randrange(100)]

here's where sample is useful: Pick a TLA, but don't repeat any letters: 'CLA'
You can't sample more than your whole population: ValueError: sample larger than population
And here's another way to take a random permutation of the alphabet: 'STFVCQHDLOUNERPYIMABGJXKZW'
No letters are repeated, unlike with random.choice:
>>> "".join([random.choice(string.uppercase) for i in
>>> range(26)])
'ZMXYCNLDCQAIYTAVPPTSGABNVU'
(the odds of *not* having a duplicate letter here are quite small,
though I don't know offhand just how small--maybe 26!/(26**25)?)

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top