Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22159 > unrolled thread
| Started by | Michael Poeltl <michael.poeltl@univie.ac.at> |
|---|---|
| First post | 2012-03-26 08:40 +0200 |
| Last post | 2012-03-26 10:36 +0100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: random number Michael Poeltl <michael.poeltl@univie.ac.at> - 2012-03-26 08:40 +0200
Re: random number Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-26 07:24 +0000
Re: random number Grzegorz Staniak <gstaniak@gmail.com> - 2012-03-26 07:50 +0000
Re: random number Robert Kern <robert.kern@gmail.com> - 2012-03-26 10:36 +0100
| From | Michael Poeltl <michael.poeltl@univie.ac.at> |
|---|---|
| Date | 2012-03-26 08:40 +0200 |
| Subject | Re: random number |
| Message-ID | <mailman.980.1332744526.3037.python-list@python.org> |
* Nikhil Verma <varma.nikhil22@gmail.com> [2012-03-26 08:09]:
> Hi All
>
> How can we generate a 6 digit random number from a given number ?
what about this?
>>> given_number=123456
>>> def rand_given_number(x):
... s = list(str(x))
... random.shuffle(s)
... return int(''.join(s))
...
>>> print (rand_given_number(given_number))
653421
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-03-26 07:24 +0000 |
| Message-ID | <4f7019b6$0$29968$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #22159 |
On Mon, 26 Mar 2012 08:40:00 +0200, Michael Poeltl wrote:
> * Nikhil Verma <varma.nikhil22@gmail.com> [2012-03-26 08:09]:
>> Hi All
>>
>> How can we generate a 6 digit random number from a given number ?
> what about this?
>
>>>> given_number=123456
>>>> def rand_given_number(x):
> ... s = list(str(x))
> ... random.shuffle(s)
> ... return int(''.join(s))
> ...
>>>> print (rand_given_number(given_number))
> 653421
That's not very random. In fact, it is *terrible* as a random number
generator.
A truly random six digit number will include any number between 100000
through 999999. There are exactly 900000 (nine hundred thousand) such
numbers.
The way you generate the not-quite-random numbers, you miss out on almost
all of them. E.g. you can generate 123456 but not 123455 or 123457.
In total, you generate only 6! = 6*5*4*3*2*1 = 720 numbers, no matter how
many millions of times you call the function. Here is a demonstration:
>>> given = 123456
>>> def rand_num(x):
... s = list(str(x))
... random.shuffle(s)
... return int(''.join(s))
...
>>> import random
>>> results = set()
>>> for i in range(10**7):
... results.add(rand_num(given))
...
>>> len(results)
720
So slightly more than 99% of all the six digit numbers will never be
generated using your method.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Grzegorz Staniak <gstaniak@gmail.com> |
|---|---|
| Date | 2012-03-26 07:50 +0000 |
| Message-ID | <jkp73e$9qd$1@mx1.internetia.pl> |
| In reply to | #22161 |
On 26.03.2012, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wroted:
>>> How can we generate a 6 digit random number from a given number ?
>> what about this?
>>
>>>>> given_number=123456
>>>>> def rand_given_number(x):
>> ... s = list(str(x))
>> ... random.shuffle(s)
>> ... return int(''.join(s))
>> ...
>>>>> print (rand_given_number(given_number))
>> 653421
>
>
> That's not very random. In fact, it is *terrible* as a random number
> generator.
But isn't it what the OP requested, i.e. "6 digit random number
*from a given number*"? That is, a random permutation of the set
of its digits?
GS
--
Grzegorz Staniak <gstaniak _at_ gmail [dot] com>
[toc] | [prev] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2012-03-26 10:36 +0100 |
| Message-ID | <mailman.988.1332754589.3037.python-list@python.org> |
| In reply to | #22162 |
On 3/26/12 8:50 AM, Grzegorz Staniak wrote:
> On 26.03.2012, Steven D'Aprano<steve+comp.lang.python@pearwood.info> wroted:
>
>>>> How can we generate a 6 digit random number from a given number ?
>>> what about this?
>>>
>>>>>> given_number=123456
>>>>>> def rand_given_number(x):
>>> ... s = list(str(x))
>>> ... random.shuffle(s)
>>> ... return int(''.join(s))
>>> ...
>>>>>> print (rand_given_number(given_number))
>>> 653421
>>
>>
>> That's not very random. In fact, it is *terrible* as a random number
>> generator.
>
> But isn't it what the OP requested, i.e. "6 digit random number
> *from a given number*"? That is, a random permutation of the set
> of its digits?
I would consider that to be a very odd interpretation of that request. But it
*is* an extraordinarily vague request. I'm not sure if even the OP knows what he
wants. I suspect he really wants something like a hash.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web