Shuffle all rows randomly in place
@param[in,out] df The data frame to shuffle @param[in] seed Optional random seed
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(data_frame), | intent(inout) | :: | df | |||
integer, | intent(in), | optional | :: | seed |
subroutine df_shuffle(df, seed) type(data_frame), intent(inout) :: df integer, intent(in), optional :: seed integer, dimension(:), allocatable :: indices, seed_array integer :: i, j, temp, seed_size real :: rand_val if (df % nrows() < 2) return ! Initialize random seed if provided if (present(seed)) then call random_seed(size=seed_size) allocate (seed_array(seed_size)) seed_array = seed call random_seed(put=seed_array) deallocate (seed_array) end if ! Create array of indices allocate (indices(df % nrows())) do i = 1, df % nrows() indices(i) = i end do ! Fisher-Yates shuffle do i = df % nrows(), 2, -1 call random_number(rand_val) j = int(rand_val * i) + 1 temp = indices(i) indices(i) = indices(j) indices(j) = temp end do ! Reorder all columns according to shuffled indices call reorder_all_columns(df, indices) deallocate (indices) end subroutine df_shuffle