[UP]


Manual Reference Pages  - scramble (3)

NAME

scramble(3f) - [M_random] generate an integer array of specified size populated with a random permutation of 1 to size(array) (LICENSE:MIT)

CONTENTS

Synopsis
Description
Options
Returns
Example
Author
License

SYNOPSIS

function scramble( number_of_values ) integer,intent(in) :: number_of_values

DESCRIPTION

Return an integer array of the size specified populated with the numbers 1 to "number_of_values" in random order.

A simple way to randomly scramble a list of any type is to create a random permutation of all the index values of the array and then access the original list elements using that list of indices. The list itself can be re-ordered very succinctly using array syntax. Given a list size ..
1. create an INTEGER array of the specified size N
2. populate it with the values from 1 to N
3. randomly switch values in the array to randomize it
4. return the newly created array for use as indices
The resulting random permutation of the indices can then be used to access essentially any type of list in random order.

OPTIONS

number_of_values
  size of integer array to create

RETURNS

scramble
  Integer array filled with integers 1 to NUMBER_OF_VALUES in random order

EXAMPLE

Sample program

    program demo_scramble
    use M_random, only : scramble
    implicit none
    character(len=*),parameter :: list(*)=[character(len=5) :: &
    & ’one’,’two’,’three’,’four’,’five’,&
    & ’six’,’seven’,’eight’,’nine’,’ten’]
    integer                    :: i
    integer                    :: n=size(list)
    character(len=len(list))   :: newlist(size(list))
    do i = 1,8
       ! use random values as indices to randomize array
       newlist=list(scramble(n))
       write(*,’(*(a,1x))’) newlist
    enddo
    end program demo_scramble

Example output

   ten   six   eight one   four  nine  two   five  three seven
   three five  ten   nine  one   four  six   seven two   eight
   four  eight ten   two   seven nine  six   three one   five
   three one   nine  seven ten   five  two   six   eight four
   two   seven nine  one   four  three eight ten   five  six
   three one   nine  six   ten   five  eight two   four  seven
   four  five  six   eight one   ten   three nine  seven two
   three nine  four  two   one   seven ten   five  six   eight

AUTHOR

John S. Urban

LICENSE

MIT License


scramble (3) October 17, 2020
Generated by manServer 1.08 from 84df46ed-ef37-483a-9845-15db97f1b955 using man macros.