Re: [go-nuts] Re: Shuffle Items in a Slice

2016-06-28 Thread Kaveh Shahbazian
Thanks for the reference! BTW I love this behaviour!

On Tue, Jun 28, 2016, 00:14 Chris Hines  wrote:

> The history of this behavior is explained in the release notes for Go 1.3:
> https://golang.org/doc/go1.3#map
>
>
> On Friday, June 24, 2016 at 8:48:20 AM UTC-4, Val wrote:
>>
>> 2 implementations here :
>> http://www.programming-idioms.org/idiom/10/shuffle-a-list/1564/go
>>
>> As for the map iteration trick, the runtime doesn't guarantee to
>> randomize anything, although it often tries to, so developers don't rely on
>> some specific order. I've seen (in the past) some map iterations
>> consistently not randomized at all. This behaviour may have evolved, but
>> don't rely on it.
>>
>>
>> On Friday, June 24, 2016 at 1:05:49 PM UTC+2, dc0d wrote:
>>>
>>> Hi;
>>>
>>> To shuffle items in a slice I'm doing this:
>>>
>>> var res []Item
>>>
>>> //fill res logic
>>>
>>> shuffle := make(map[int]*Item)
>>> for k, v := range res {
>>>  shuffle[k] = 
>>> }
>>> res = nil
>>> for _, v := range shuffle {
>>>  res = append(res, *v)
>>> }
>>>
>>> Which inserts items into a map then ranges over that map. Ranging over a
>>> map in Go returns the items in random order.
>>>
>>> 1 - I thought it's a cool trick!
>>> 2 - Is anything bad about doing this?
>>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/UIYHKFeIf_k/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-- 
Regards,
Kaveh Shahbazian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Shuffle Items in a Slice

2016-06-27 Thread Chris Hines
The history of this behavior is explained in the release notes for Go 
1.3: https://golang.org/doc/go1.3#map

On Friday, June 24, 2016 at 8:48:20 AM UTC-4, Val wrote:
>
> 2 implementations here : 
> http://www.programming-idioms.org/idiom/10/shuffle-a-list/1564/go
>
> As for the map iteration trick, the runtime doesn't guarantee to randomize 
> anything, although it often tries to, so developers don't rely on some 
> specific order. I've seen (in the past) some map iterations consistently 
> not randomized at all. This behaviour may have evolved, but don't rely on 
> it.
>
>
> On Friday, June 24, 2016 at 1:05:49 PM UTC+2, dc0d wrote:
>>
>> Hi;
>>
>> To shuffle items in a slice I'm doing this:
>>
>> var res []Item
>>
>> //fill res logic
>>
>> shuffle := make(map[int]*Item)
>> for k, v := range res {
>>  shuffle[k] = 
>> }
>> res = nil
>> for _, v := range shuffle {
>>  res = append(res, *v)
>> }
>>
>> Which inserts items into a map then ranges over that map. Ranging over a 
>> map in Go returns the items in random order.
>>
>> 1 - I thought it's a cool trick!
>> 2 - Is anything bad about doing this?
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: Shuffle Items in a Slice

2016-06-27 Thread Martin Geisler
Hi Val

On Fri, Jun 24, 2016 at 2:48 PM, Val  wrote:
> 2 implementations here :
> http://www.programming-idioms.org/idiom/10/shuffle-a-list/1564/go
>
> As for the map iteration trick, the runtime doesn't guarantee to randomize
> anything, although it often tries to, so developers don't rely on some
> specific order.

That's a nice feature and very helpful when writing tests... otherwise
you end up with a test that works fine on one version/architecture and
"suddenly" breaks later.

Randomizing the order "on purpose" is probably also done for security
reasons. Python works much the same, and some years ago, the CPython
VM began randomizing the dict (map) type. The problem was that
carefully crafted input would trigger a worst-case scenario in the
hash table used behind the scenes and suddenly make your server crawl
to a halt.

The input could be HTTP headers, for example, with carefully chosen
names: these are normally read from the client and used as keys in a
map. With the right keys, you can trigger hash collisions and make the
map allocate much more memory than you would expect with typical keys.

Having the runtime add a bit of randomness to the keys prevents this
scenario. When the randomness changes at every execution, it further
helps the developers by highlighting the nondeterministic iteration
order of the hash table.

> I've seen (in the past) some map iterations consistently not
> randomized at all. This behaviour may have evolved, but don't rely on it.

That's good advice :-)

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Shuffle Items in a Slice

2016-06-24 Thread Val
2 implementations here : 
http://www.programming-idioms.org/idiom/10/shuffle-a-list/1564/go

As for the map iteration trick, the runtime doesn't guarantee to randomize 
anything, although it often tries to, so developers don't rely on some 
specific order. I've seen (in the past) some map iterations consistently 
not randomized at all. This behaviour may have evolved, but don't rely on 
it.


On Friday, June 24, 2016 at 1:05:49 PM UTC+2, dc0d wrote:
>
> Hi;
>
> To shuffle items in a slice I'm doing this:
>
> var res []Item
>
> //fill res logic
>
> shuffle := make(map[int]*Item)
> for k, v := range res {
>  shuffle[k] = 
> }
> res = nil
> for _, v := range shuffle {
>  res = append(res, *v)
> }
>
> Which inserts items into a map then ranges over that map. Ranging over a 
> map in Go returns the items in random order.
>
> 1 - I thought it's a cool trick!
> 2 - Is anything bad about doing this?
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.