# NArray shuffle

**URL:** <https://www.ruby-forum.com/t/narray-shuffle/67785>\
**Category:** Ruby\
**Created:** [September 6, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/narray-shuffle/67785 "2006-09-06T20:08:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [September 6, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/narray-shuffle/67785/1 "2006-09-06T20:08:45Z")

</div>

Has anyone ever needed to “shuffle” an NArray? I can certainly convert  
it to an array and do the sort\_by {rand} idiom, and then convert it  
back. I currently:

na.size.times do  
p1,p2 = rand(na.size),rand(na.size)  
na[p1],na[p2] = na[p2],na[p1]  
end

which works, but I wondering if anyone knew a better way.  
Thanks  
pth

---

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [September 6, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/narray-shuffle/67785/2 "2006-09-06T22:31:03Z")

</div>

On Thu, 7 Sep 2006, Patrick H. wrote:

> Thanks  
> pth

if your na is not huge you might use:

```
 harp:~ > cat a.rb
 require 'narray'

 na = NArray.int(3,2).indgen!

 p na

 idx = NArray.to_na Array.new(na.size){|i| i}.sort_by{ rand }

 na[] = na[idx].reshape *na.shape

 p na

 harp:~ > ruby a.rb
 NArray.int(3,2):
 [[ 0, 1, 2],
   [3, 4, 5] ]
 NArray.int(3,2):
 [[ 0, 3, 1],
   [2, 4, 5] ]

```

which at least does it in one hit at the cost of space.

-a

---

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [September 6, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/narray-shuffle/67785/3 "2006-09-06T23:05:05Z")

</div>

“Patrick H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Has anyone ever needed to “shuffle” an NArray? I can certainly convert  
> it to an array and do the sort\_by {rand} idiom, and then convert it  
> back. I currently:

What about:

shuffled = orig[NArray.float(\*orig.shape).random!.sort\_index]

---

<div class="post-metadata">

**Author:** ![patrickharlan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/77aa72/32.png) [@patrickharlan](https://www.ruby-forum.com/u/patrickharlan)\
**Post date:** [September 7, 2006, 12:27am UTC](https://www.ruby-forum.com/t/narray-shuffle/67785/4 "2006-09-07T00:27:17Z")

</div>

On 9/6/06, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> s=%q( Daniel M. – removed\_email\_address@domain.invalid  
> puts “s=%q(#{s})”,s.map{|i|i}[1] )  
> puts “s=%q(#{s})”,s.map{|i|i}[1]

Thanks Daniel, that is perfect – I did not know you could index an NA  
with an Array that way – its perfect for my needs (plus it keeps the  
type of the Array).

pth
