Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help how to sort a list in order of 'n' in python without using inbuilt functions??

82 views
Skip to first unread message

lokesh...@gmail.com

unread,
May 24, 2013, 4:04:51 AM5/24/13
to
i need to write a code which can sort the list in order of 'n' without use builtin functions
can anyone help me how to do?

Chris Angelico

unread,
May 24, 2013, 4:12:45 AM5/24/13
to pytho...@python.org
On Fri, May 24, 2013 at 6:04 PM, <lokesh...@gmail.com> wrote:
> i need to write a code which can sort the list in order of 'n' without use builtin functions
> can anyone help me how to do?
> --
> http://mail.python.org/mailman/listinfo/python-list

http://lmgtfy.com/?q=sorting+algorithm
http://www.catb.org/esr/faqs/smart-questions.html

ChrisA

Dave Angel

unread,
May 24, 2013, 8:33:57 AM5/24/13
to pytho...@python.org
On 05/24/2013 04:04 AM, lokesh...@gmail.com wrote:
> i need to write a code which can sort the list in order of 'n' without use builtin functions
> can anyone help me how to do?
>

You could sort, but you couldn't print out the results, so what's the
point? In Python 3.3 at least, print() is a built-in function.

Is the homework assignment more clearly worded than your summary? And
if so, how far have you gotten on it?

--
DaveA

Carlos Nepomuceno

unread,
May 24, 2013, 9:06:59 AM5/24/13
to pytho...@python.org
lol wtf?

If 'n' is the quantity of elements to be sorted there's
no way you can write an algorithm with complexity O(n) for the worst
case not knowing something special about the data.

For example, Quicksort will give you O(n*(log(n)) on average case (O(n^2) in the worst case).

You gotta be much more specific than that if you really need an O(n) code.

There's probably assumptions you didn't mention about the data if O(n) it's really what you're looking for.

PS: Looks like a joke as stated..

----------------------------------------
> Date: Fri, 24 May 2013 01:04:51 -0700
> Subject: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: lokesh...@gmail.com
> To: pytho...@python.org


>
> i need to write a code which can sort the list in order of 'n' without use builtin functions
> can anyone help me how to do?

> --
> http://mail.python.org/mailman/listinfo/python-list

lokesh...@gmail.com

unread,
May 25, 2013, 1:15:21 AM5/25/13
to
On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh...@gmail.com wrote:
> i need to write a code which can sort the list in order of 'n' without use builtin functions
>
> can anyone help me how to do?

Note:
the list only contains 0's,1's,2's
need to sort them in order of 'n'

Chris Angelico

unread,
May 25, 2013, 1:24:01 AM5/25/13
to pytho...@python.org
In that case, you're not really ordering them, you're counting them.
Look at the collections module; you can very easily figure out how
many of each there are, and then reconstruct the list afterward.

ChrisA

lokesh...@gmail.com

unread,
May 25, 2013, 1:39:06 AM5/25/13
to
but i need to do it with out using builtin functions

rusi

unread,
May 25, 2013, 1:43:18 AM5/25/13
to

Chris Angelico

unread,
May 25, 2013, 1:43:30 AM5/25/13
to pytho...@python.org
Depending on your definitions, that's either trivially easy (the
'collections' module is not builtin functions) or completely
impossible. Have fun.

ChrisA

Steven D'Aprano

unread,
May 25, 2013, 1:57:38 AM5/25/13
to
> but i need to do it with out using builtin functions


How would you, an intelligent human being count them?

Describe how you would count them in English, or whatever your native
language is.

"First I would start a tally for the number of zeroes, tally = 0. Then I
look at each item in turn, and if it is 0, I add one to the tally. When I
get to the end, I the number of zeroes is equal to the tally.

Then I do the same for the number of ones, and the number of twos."

Now turn that into Python code.

tally = 0
for item in list_of_items:
if item == 0:
tally = tally + 1

print "The number of zeroes equals", tally



--
Steven

lokesh...@gmail.com

unread,
May 25, 2013, 2:05:17 AM5/25/13
to
ya steven i had done the similar logic but thats not satisfying my professor
he had given the following constrains
1. No in-built functions should be used
2. we are expecting a O(n) solution
3. Don't use count method

Chris Angelico

unread,
May 25, 2013, 2:12:24 AM5/25/13
to pytho...@python.org
On Sat, May 25, 2013 at 4:05 PM, <lokesh...@gmail.com> wrote:
> ya steven i had done the similar logic but thats not satisfying my professor
> he had given the following constrains
> 1. No in-built functions should be used
> 2. we are expecting a O(n) solution
> 3. Don't use count method

And now you finally admit that it's homework.

Solve it yourself, or go to your prof and say that you need help.

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 3:53:19 AM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Fri, 24 May 2013 23:05:17 -0700
> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: lokesh...@gmail.com
> To: pytho...@python.org
[...]

> ya steven i had done the similar logic but thats not satisfying my professor
> he had given the following constrains
> 1. No in-built functions should be used
> 2. we are expecting a O(n) solution
> 3. Don't use count method

That's trivial!

# l is a list of numbers: 0,1,2
def order_n(l):
    r = []
    count = [0]*3
    count[2] = len(l)
    for i in range(len(l)):
        count[1] += abs((l[i]>0)-1)
        r.insert(count[l[i]], l[i])
    return r

'count' is a list I've defined to count the quantity of the elements in the argument, not the method.

I don't think it needs any explanations, but if you have any doubts just ask. ;)

Good luck!

Chris Angelico

unread,
May 25, 2013, 4:28:32 AM5/25/13
to pytho...@python.org
On Sat, May 25, 2013 at 5:53 PM, Carlos Nepomuceno
<carlosne...@outlook.com> wrote:
> ----------------------------------------
>> Date: Fri, 24 May 2013 23:05:17 -0700
>> 1. No in-built functions should be used
> count[2] = len(l)

Fail! :)

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 4:43:55 AM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Sat, 25 May 2013 18:28:32 +1000

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org

>
> On Sat, May 25, 2013 at 5:53 PM, Carlos Nepomuceno
> <carlosne...@outlook.com> wrote:
>> ----------------------------------------
>>> Date: Fri, 24 May 2013 23:05:17 -0700
>>> 1. No in-built functions should be used
>> count[2] = len(l)
>
> Fail! :)
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list

lol I forgot to include this monkey patch! ;)

def length(l):
    x=0
    y=l[:]
    while y:
        x+=1
        y.pop()
    return x

Chris Angelico

unread,
May 25, 2013, 4:47:24 AM5/25/13
to pytho...@python.org
On Sat, May 25, 2013 at 6:43 PM, Carlos Nepomuceno
<carlosne...@outlook.com> wrote:
> ----------------------------------------
> lol I forgot to include this monkey patch! ;)
>
> def length(l):
> x=0
> y=l[:]
> while y:
> x+=1
> y.pop()
> return x

Nice. Now eliminate abs (easy) and range. :)

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 4:54:53 AM5/25/13
to pytho...@python.org
lol

def absolute(x):
    return x if x>0 else -x

def reach(x):
    y=[]
    z=0
    while z<x:
        y.append(z)
        z+=1
    return y

----------------------------------------
> Date: Sat, 25 May 2013 18:47:24 +1000


> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org
>

> --
> http://mail.python.org/mailman/listinfo/python-list

Chris Angelico

unread,
May 25, 2013, 5:01:09 AM5/25/13
to pytho...@python.org
On Sat, May 25, 2013 at 6:54 PM, Carlos Nepomuceno
<carlosne...@outlook.com> wrote:
> lol
>
> def absolute(x):
> return x if x>0 else -x
>
> def reach(x):
> y=[]
> z=0
> while z<x:
> y.append(z)
> z+=1
> return y

Very good. You are now in a position to get past the limitations of a
restricted-environment eval/exec. Avoiding builtins is actually a fun
skill to hone.

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 5:10:06 AM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Sat, 25 May 2013 19:01:09 +1000

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org
[...]

> Very good. You are now in a position to get past the limitations of a
> restricted-environment eval/exec. Avoiding builtins is actually a fun
> skill to hone.
>
> ChrisA


I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;)

Chris Angelico

unread,
May 25, 2013, 5:14:57 AM5/25/13
to pytho...@python.org
def random_number():
return 7

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 5:30:28 AM5/25/13
to pytho...@python.org
lol http://search.dilbert.com/comic/Random%20Nine

----------------------------------------
> Date: Sat, 25 May 2013 19:14:57 +1000


> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org
>
> On Sat, May 25, 2013 at 7:10 PM, Carlos Nepomuceno
> <carlosne...@outlook.com> wrote:
>> ----------------------------------------
>>> Date: Sat, 25 May 2013 19:01:09 +1000
>>> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
>>> From: ros...@gmail.com
>>> To: pytho...@python.org
>> [...]
>>> Very good. You are now in a position to get past the limitations of a
>>> restricted-environment eval/exec. Avoiding builtins is actually a fun
>>> skill to hone.
>>>
>>> ChrisA
>>
>>
>> I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;)
>
> def random_number():
> return 7
>
> ChrisA

> --
> http://mail.python.org/mailman/listinfo/python-list

Mark Lawrence

unread,
May 25, 2013, 8:01:06 AM5/25/13
to pytho...@python.org
On 25/05/2013 09:54, Carlos Nepomuceno wrote:
> lol
>
> def absolute(x):
> return x if x>0 else -x
>
> def reach(x):
> y=[]
> z=0
> while z<x:
> y.append(z)
> z+=1
> return y
>

In my book this is another fail as lists are inbuilt (yuck!) and so is
the add function that'll be called for z+=1.

--
If you're using GoogleCrap� please read this
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

Roy Smith

unread,
May 25, 2013, 9:29:57 AM5/25/13
to
In article <78192328-b31b-49d9...@googlegroups.com>,
What do you mean by "need to sort them in order of 'n'". Are you saying
that you need to sort them into numerical order? Or that you need to
running time of the algorithm to be O(n)?

I'm assuming the later, in which case this is starting to sound like a
classic interview question.

Assuming you can accept an unstable sort, there's an easy solution. I'm
not aware of any solution if you require a stable sort. Perhaps that's
enough of a hint?

Roy Smith

unread,
May 25, 2013, 10:03:24 AM5/25/13
to
In article <74e33270-a79a-4878...@googlegroups.com>,
lokesh...@gmail.com wrote:

> ya steven i had done the similar logic but thats not satisfying my professor
> he had given the following constrains
> 1. No in-built functions should be used
> 2. we are expecting a O(n) solution
> 3. Don't use count method

A couple of points here:

1) In general, people on mailing lists are not into doing homework
problems for other people.

2) If you're going to bring us a homework problem, at least describe the
whole problem up front. It really doesn't help to dribble out new
requirements one at a time.

3) rusto...@gmail.com already posted a pointer to the wikipedia
article describing the required algorithm in detail.

4) I don't know what "no built-in functions should be used" means. I
assume it means, "don't call sort()"? If you can't even call
int.__lt__(), it's going to be really hard to do this.

Dave Angel

unread,
May 25, 2013, 10:27:02 AM5/25/13
to pytho...@python.org
The OP has already admitted that he didn't want a sort at all. He wants
to COUNT the items, not sort them. So nothing in his orginal post
relates to the real homework assignment.

--
DaveA

Steven D'Aprano

unread,
May 25, 2013, 10:28:33 AM5/25/13
to
On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote:

> def random_number():
> return 7

I call shenanigans! That value isn't generated randomly, you just made it
up! I rolled a die *hundreds* of times and not once did it come up seven!



--
Steven

Steven D'Aprano

unread,
May 25, 2013, 10:30:22 AM5/25/13
to
On Fri, 24 May 2013 23:05:17 -0700, lokeshkoppaka wrote:

> On Saturday, May 25, 2013 11:27:38 AM UTC+5:30, Steven D'Aprano wrote:

>> tally = 0
>> for item in list_of_items:
>> if item == 0:
>> tally = tally + 1
>>
>> print "The number of zeroes equals", tally
>
>
> ya steven i had done the similar logic but thats not satisfying my
> professor he had given the following constrains
> 1. No in-built functions should be used

The above does not use any built-in functions.

> 2. we are expecting a O(n) solution

The above is O(n).

> 3. Don't use count method

The above does not use the count method.



--
Steven

Fábio Santos

unread,
May 25, 2013, 10:46:46 AM5/25/13
to Steven D'Aprano, pytho...@python.org

> --
> http://mail.python.org/mailman/listinfo/python-list

Try flipping a coin. I flipped one a couple of times and got the seventh face once.

Jussi Piitulainen

unread,
May 25, 2013, 10:59:30 AM5/25/13
to
Surely it's assumed that each element of the input list can be
classified as 0, 1, or 2 in O(1) time. If O(n) auxiliary space can be
allocated in O(n) time, just put the 0's in their own list in the
order they are encountered, 1's in their own list in the order they
are encountered, 2's in their own list in the order they are
encountered, then put the 0's back into the input list in the order
they are encountered in their auxiliary list, followed by the 1's,
followed by the 2's. Stable and O(n), no?

Even ( [ x for x in input if x.key == 0 ] +
[ x for x in input if x.key == 1 ] +
[ x for x in input if x.key == 2 ] ).
I suppose a list comprehension is not technically a function any more
than a loop is.

(Neither stability nor O(1) space has been required yet, I think.)

Mark Lawrence

unread,
May 25, 2013, 11:03:44 AM5/25/13
to pytho...@python.org
Lies, damn lies and statistics? :)

Chris Angelico

unread,
May 25, 2013, 11:41:58 AM5/25/13
to pytho...@python.org
You've obviously never used a REAL set of dice. Now, I have here with
me a set used for maths drill (to be entirely accurate, what I have
here is the company's stock of them, so there are multiples of each of
these - anyone need to buy dice?) with everything except the classic 1
through 6 that everyone knows:

* Six sides, faces marked 7 through 12
* Six sides, faces marked "+x-\xf7+" and a "wild" marker (yes, two of +)
* Ten sides, numbered 0 through 9
* Eight sides, numbered 1 through 8
* Twelve sides, as above
* Twenty sides, as above

Now, tabletop roleplayers will recognize the latter four as the ones
notated as d10, d8, d12, and d20, but these are NOT for gameplay, they
are for serious educational purposes! Honest!

Anyway, all of those can roll a 7... well, one of them has to roll a
\xf7, but close enough right? Plus, if you roll 2d6 (that is, two
regular six-sided dice and add them up), 7 is statistically the most
likely number to come up with. Therefore it IS random.

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 1:07:47 PM5/25/13
to pytho...@python.org
----------------------------------------
> To: pytho...@python.org
> From: bream...@yahoo.co.uk

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> Date: Sat, 25 May 2013 13:01:06 +0100
[...]

> In my book this is another fail as lists are inbuilt (yuck!) and so is
> the add function that'll be called for z+=1.

Indeed! It's a joke Mark! lol
;)

Carlos Nepomuceno

unread,
May 25, 2013, 1:12:44 PM5/25/13
to pytho...@python.org
----------------------------------------
> From: steve+comp....@pearwood.info

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> Date: Sat, 25 May 2013 14:28:33 +0000
> To: pytho...@python.org


lol It worked fine on my d8! lol

Carlos Nepomuceno

unread,
May 25, 2013, 1:17:51 PM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Sun, 26 May 2013 01:41:58 +1000

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org
>
> On Sun, May 26, 2013 at 12:28 AM, Steven D'Aprano
> <steve+comp....@pearwood.info> wrote:
> You've obviously never used a REAL set of dice. Now, I have here with
> me a set used for maths drill (to be entirely accurate, what I have
> here is the company's stock of them, so there are multiples of each of
> these - anyone need to buy dice?) with everything except the classic 1
> through 6 that everyone knows:
>
> * Six sides, faces marked 7 through 12
> * Six sides, faces marked "+x-\xf7+" and a "wild" marker (yes, two of +)
> * Ten sides, numbered 0 through 9
> * Eight sides, numbered 1 through 8
> * Twelve sides, as above
> * Twenty sides, as above
>
> Now, tabletop roleplayers will recognize the latter four as the ones
> notated as d10, d8, d12, and d20, but these are NOT for gameplay, they
> are for serious educational purposes! Honest!
>
> Anyway, all of those can roll a 7... well, one of them has to roll a
> \xf7, but close enough right? Plus, if you roll 2d6 (that is, two
> regular six-sided dice and add them up), 7 is statistically the most
> likely number to come up with. Therefore it IS random.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list


def f(x):
    return x+1

or you can just go:

f(roll_d6())

;)

Chris Angelico

unread,
May 25, 2013, 1:23:44 PM5/25/13
to pytho...@python.org
On Sun, May 26, 2013 at 3:17 AM, Carlos Nepomuceno
<carlosne...@outlook.com> wrote:
> def f(x):
> return x+1
>
> or you can just go:
>
> f(roll_d6())

Hmm. Interesting. So now we have a question: Does adding 1 to a random
number make it less random? It adds determinism to the number; can a
number be more deterministic while still no less random?

Ah! I know. The answer comes from common sense:

a = random() # a is definitely a random number
a -= random() # a is no longer random, we subtracted all the randomness from it

Of course, since number-number => number, a is still a number. And so
we can conclude that adding 1 to a random dice roll does indeed leave
all the randomness still in it. But wait! That means we can do
better!!

a = random() # a is a random number
a *= 5 # a is clearly five times as random now!

ChrisA

Carlos Nepomuceno

unread,
May 25, 2013, 1:34:02 PM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Sun, 26 May 2013 03:23:44 +1000

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: ros...@gmail.com
> To: pytho...@python.org
>
> --
> http://mail.python.org/mailman/listinfo/python-list

It depends if the result (any operation) is in the required range.

For example: "int(random.random()+1)" it's not random at all!

But "int(random.random()*1000)" my look random if it fits your needs.

;)

Carlos Nepomuceno

unread,
May 25, 2013, 1:45:40 PM5/25/13
to pytho...@python.org
----------------------------------------
> Date: Fri, 24 May 2013 23:05:17 -0700

> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: lokesh...@gmail.com
[...]

> ya steven i had done the similar logic but thats not satisfying my professor
> he had given the following constrains
> 1. No in-built functions should be used

> 2. we are expecting a O(n) solution

> 3. Don't use count method


PS: If you find something faster please let me know!

Steven D'Aprano

unread,
May 25, 2013, 11:09:51 PM5/25/13
to
On Sun, 26 May 2013 01:41:58 +1000, Chris Angelico wrote:

> On Sun, May 26, 2013 at 12:28 AM, Steven D'Aprano
> <steve+comp....@pearwood.info> wrote:
>> On Sat, 25 May 2013 19:14:57 +1000, Chris Angelico wrote:
>>
>>> def random_number():
>>> return 7
>>
>> I call shenanigans! That value isn't generated randomly, you just made
>> it up! I rolled a die *hundreds* of times and not once did it come up
>> seven!
>
> You've obviously never used a REAL set of dice.

You're right, all my dice are eight-sided and complex:

1+0i
1+1i
1-1i
-1+0i
-1+1i
-1-1i


:-)


But seriously, I have various D&D style gaming dice, d4, d6, d8, d12, d20
and d30. But I thought the opportunity for a joke was more important than
pedantic correctness :-)


> Now, I have here with me
> a set used for maths drill (to be entirely accurate, what I have here is
> the company's stock of them, so there are multiples of each of these -
> anyone need to buy dice?)

Are you serious? What's the cost, posted to Melbourne?


> with everything except the classic 1 through 6 that everyone knows:
>
> * Six sides, faces marked 7 through 12
> * Six sides, faces marked "+x-\xf7+" and a "wild" marker
> (yes, two of +)

Oh, you mean ÷ (division sign)! Why didn't you say so? :-P

And another thing, shame on you, you mean × not x. It's easy to find too:

py> from unicodedata import lookup
py> print(lookup("MULTIPLICATION SIGN"))
×


> * Ten sides, numbered 0 through 9
> * Eight sides, numbered 1 through 8
> * Twelve sides, as above
> * Twenty sides, as above
>
> Now, tabletop roleplayers will recognize the latter four as the ones
> notated as d10, d8, d12, and d20, but these are NOT for gameplay, they
> are for serious educational purposes! Honest!
>
> Anyway, all of those can roll a 7... well, one of them has to roll a
> \xf7, but close enough right?

I don't think so...

> Plus, if you roll 2d6 (that is, two
> regular six-sided dice and add them up), 7 is statistically the most
> likely number to come up with. Therefore it IS random.

Yes, but if you subtract them the most common is 0, if you multiply the
most common are 6 or 12, and if you divide the most common is 1. If you
decide on the operation randomly, using the +-×÷+ die above (ignoring
wildcards), the most common result is 6. The probability of getting a 7
is just 1/15.

from collections import Counter
from operator import add, sub, mul, truediv as div
ops = (add, sub, mul, div, add)
Counter(op(i, j) for op in ops for i in range(1, 7) for j in range(1, 7))


--
Steven

Steven D'Aprano

unread,
May 25, 2013, 11:38:12 PM5/25/13
to
On Sun, 26 May 2013 03:23:44 +1000, Chris Angelico wrote:

> Does adding 1 to a random
> number make it less random? It adds determinism to the number; can a
> number be more deterministic while still no less random?
>
> Ah! I know. The answer comes from common sense:
[snip spurious answer]

I know you're being funny, but in fact adding a constant to a random
variable still leaves it equally random. Adding, multiplying, dividing or
subtracting a constant from a random variable X just shifts the possible
values X can take, it doesn't change the shape of the distribution.
However, adding two random variables X and Y does change the
distribution. In fact, a very cheap way of simulating an almost normally
distributed random variable is to add up a whole lot of uniformly
distributed random variables. Adding up 12 calls to random.random(), and
subtracting 6, gives you a close approximation to a Gaussian random
variable with mean 0 and standard deviation 1.



--
Steven

Chris Angelico

unread,
May 26, 2013, 12:02:29 AM5/26/13
to pytho...@python.org
On Sun, May 26, 2013 at 1:09 PM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> You're right, all my dice are eight-sided and complex:
>
> 1+0i
> 1+1i
> 1-1i
> -1+0i
> -1+1i
> -1-1i
>
>
> :-)

Now THAT is a dice of win!

>> Now, I have here with me
>> a set used for maths drill (to be entirely accurate, what I have here is
>> the company's stock of them, so there are multiples of each of these -
>> anyone need to buy dice?)
>
> Are you serious? What's the cost, posted to Melbourne?

$1 each, postage probably $5 for any number. Or there may even be
option to pick up / hand deliver, depending on where in Melb you are.

http://www.kepl.com.au/ - company's winding down, but we still have stock.

> Oh, you mean ÷ (division sign)! Why didn't you say so? :-P

I tend to stick to ASCII in these posts. :)

> And another thing, shame on you, you mean × not x. It's easy to find too:
>
> py> from unicodedata import lookup
> py> print(lookup("MULTIPLICATION SIGN"))
> ×

I'm aware of that, but see above, I stick to ASCII where possible. The
faces would be better represented with some of the other digits (the
bolded ones, perhaps), but I used the ASCII digits. :)

>> Plus, if you roll 2d6 (that is, two
>> regular six-sided dice and add them up), 7 is statistically the most
>> likely number to come up with. Therefore it IS random.
>
> Yes, but if you subtract them the most common is 0, if you multiply the
> most common are 6 or 12, and if you divide the most common is 1. If you
> decide on the operation randomly, using the +-×÷+ die above (ignoring
> wildcards), the most common result is 6. The probability of getting a 7
> is just 1/15.
>
> from collections import Counter
> from operator import add, sub, mul, truediv as div
> ops = (add, sub, mul, div, add)
> Counter(op(i, j) for op in ops for i in range(1, 7) for j in range(1, 7))

LOL! I never thought to go THAT far into the analysis..... Nice one!

ChrisA

Dan Sommers

unread,
May 26, 2013, 12:06:34 AM5/26/13
to
On Sun, 26 May 2013 03:38:12 +0000, Steven D'Aprano wrote:

> ... adding a constant to a random variable still leaves it equally
> random. Adding, multiplying, dividing or subtracting a constant from a
> random variable X just shifts the possible values X can take ...

That's mathematically true, but this is the Python mailing list.

Adding, subtracting, or dividing by a sufficiently large constant loses
all the randomness.

Python 3.2.4 (default, May 8 2013, 20:55:18)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.random() + 1e200
1e+200
>>> random.random() - 1e200
-1e+200
>>> random.random() / 1e309
0.0

But you knew that. ;-)

Dan

Chris Angelico

unread,
May 26, 2013, 12:28:37 AM5/26/13
to pytho...@python.org
On Sun, May 26, 2013 at 1:38 PM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> On Sun, 26 May 2013 03:23:44 +1000, Chris Angelico wrote:
>
>> Does adding 1 to a random
>> number make it less random? It adds determinism to the number; can a
>> number be more deterministic while still no less random?
>>
>> Ah! I know. The answer comes from common sense:
> [snip spurious answer]
>
> I know you're being funny, but in fact adding a constant to a random
> variable still leaves it equally random. Adding, multiplying, dividing or
> subtracting a constant from a random variable X just shifts the possible
> values X can take, it doesn't change the shape of the distribution.

In real numbers, that's correct. However, computers don't work with
real numbers, so there's the very, uhh, REAL possibility that some of
the entropy will be lost. For instance, multiplying and dividing when
working with integers results in truncation, and adding huge numbers
to small floats results in precision loss.

I was deliberately playing around, but unfortunately there have been
many people who've genuinely thought things similar to what I was
saying - and then implemented into code.

> However, adding two random variables X and Y does change the
> distribution. In fact, a very cheap way of simulating an almost normally
> distributed random variable is to add up a whole lot of uniformly
> distributed random variables. Adding up 12 calls to random.random(), and
> subtracting 6, gives you a close approximation to a Gaussian random
> variable with mean 0 and standard deviation 1.

Yep. The more dice you roll, the more normal the distribution. Which
means that d100 is extremely swingy, but 11d10-10 is much less so, and
99d2-98 quite stable. The more randomness you add, the more
predictable the result.

<quote source="Jubal Early">Does that seem right to you?</quote>

ChrisA
0 new messages