Re: generate multiple arrays using n-number values randomly

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Wed Dec 21 2011 - 16:22:19 MST

There is a faster method for random permutation of an array. This
does not matter for very small arrays like the current example. But
for the record, use the "modern version" of the Fisher–Yates shuffle
for large arrays or many more randomizations, described here:

  http://en.wikipedia.org/wiki/Fisher%96Yates_shuffle

The algorithm is simple and codes easily. This method is linear with
increasing length, in other words O(n). When sorting is used such as
below, time increases as O(n^2) or at least O(n log n), depending on
the efficiency of the particular sort function.

In NCL, the trade-off point may be fairly large, between a built-in
sort function written in C, such as dim_pqsort; versus a shuffle loop
written in NCL.

--Dave

On Wed, Dec 21, 2011 at 2:26 PM, Mary Haley <haley@ucar.edu> wrote:
> Oops, minor error in the debug print section of the code. The "nvals-2" in
> the do loop should be "nvals-1".
>
> --Mary
>
> On Dec 21, 2011, at 1:21 PM, Mary Haley wrote:
>
> Hi Debasish,
>
> There's a question on this in the FAQ, so I took that and modified it a
> little:
>
>   nvals = 30
>   narr  = 1000
>
> ;---Generate 1000 x 30 random numbers
>   r = random_uniform(0,1,(/narr,nvals/))  ; random numbers
>
> ;---Sorting the values and keeping the indexes gives us random indexes.
>   indexes = dim_pqsort(r, 1)
>
> ;---Debug print
>   do i=0,narr-1
>     str = ""
>     do j=0,nvals-2
>       str = str + " " + indexes(i,j)
>     end do
>     print("" + str)
>   end do
>
> You can use the "indexes" variable to index your array of 30 numbers.
>
> --Mary
>
> On Dec 21, 2011, at 1:17 PM, Debasish wrote:
>
> Dear NCL users,
>
> I have an array with 30 numbers. Now I would like to create 1000 arrays
> using this 30 numbers randomly and size of the each new array is also 30.
>
> example:  Input array   x = (x1, x2, x3, x4,......................,x30)
>
> Output 1000 arrays  X1, X2, X3,..............,X1000 and dimension of XI
> (where I =0, 1000) is 30
> where
> X1 =  (x21, x12, x3, x24,......................,x5)
> X2 =  (x1, x20, x13, x24, x24, x24, ......................,x1)
> X3 =  (x15, x25, x19, x14, x22, x10, x12,x12,......................,x7)
> .
> .
> .
> X1000 =  (x10, x29, x13, x29, x13, x15......................,x6)
>
> Could you please let me know the suitable function in NCL that solves this
> problem
>
> with regards
>
> Debasish
Received on Wed Dec 21 16:22:23 2011

This archive was generated by hypermail 2.1.8 : Wed Jan 04 2012 - 10:18:09 MST