Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. do k=1,num
  2. u=mod(16807.d0*u,2147483647.d0)
  3. v=u/2147883648.d0
  4. s=int(dble(num)*v)+1
  5. if (s.gt.num) s=1
  6. end do
  7.  
  8. ian@ian-pc:~/test/stackoverflow$ cat shuffle.f90
  9. Program shuffle
  10.  
  11. Implicit None
  12.  
  13. Integer, Parameter :: n = 10
  14.  
  15. Integer, Dimension( 1:n ) :: choices
  16.  
  17. Real :: a
  18.  
  19. Integer :: n_chosen
  20. Integer :: this
  21. Integer :: tmp
  22. Integer :: i
  23.  
  24. choices = (/ ( i, i = 1, n ) /)
  25.  
  26. n_chosen = 0
  27. Do i = 1, n
  28. Call random_number( a )
  29. this = a * ( n - n_chosen ) + 1
  30. Write( *, * ) 'Chosen ', choices( this )
  31. tmp = choices( this )
  32. choices( this ) = choices( n - n_chosen )
  33. choices( n - n_chosen ) = tmp
  34. n_chosen = n_chosen + 1
  35. End Do
  36.  
  37. End Program shuffle
  38. ian@ian-pc:~/test/stackoverflow$ nagfor shuffle.f90
  39. NAG Fortran Compiler Release 5.3.1(907)
  40. [NAG Fortran Compiler normal termination]
  41. ian@ian-pc:~/test/stackoverflow$ ./a.out
  42. Chosen 5
  43. Chosen 10
  44. Chosen 4
  45. Chosen 9
  46. Chosen 8
  47. Chosen 2
  48. Chosen 6
  49. Chosen 3
  50. Chosen 1
  51. Chosen 7
  52. ian@ian-pc:~/test/stackoverflow$ ./a.out
  53. Chosen 6
  54. Chosen 3
  55. Chosen 5
  56. Chosen 4
  57. Chosen 9
  58. Chosen 8
  59. Chosen 10
  60. Chosen 2
  61. Chosen 7
  62. Chosen 1
  63. ian@ian-pc:~/test/stackoverflow$
  64.  
  65. Program shuffle
  66. Implicit None
  67. Integer, Parameter :: num = 27
  68. Integer, Dimension( 1:num ) :: jj
  69. Real*8 uni,ir
  70. Integer :: n_chosen
  71. Integer :: this
  72. Integer :: tmp
  73. Integer :: i,ii
  74. jj = (/ ( i, i = 1, num ) /)
  75. ir=1245.d0
  76. n_chosen = 0
  77. Do i = 1, num
  78. Call ran(ir,uni)
  79. this = uni * ( num - n_chosen ) + 1
  80. ii=jj(this)
  81. tmp = jj( this )
  82. jj( this ) = jj( num - n_chosen )
  83. jj( num - n_chosen ) = tmp
  84. n_chosen = n_chosen + 1
  85. End Do
  86. end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement