No big deal but if Rnd = 0 in Sub shuffle then we get Swap a(n), a(n).
Instead of
Code: Select all
Swap a(n), a(range(n,Ubound(a)))
this should do it
Code: Select all
Swap a(n), a(range(n+1,Ubound(a)))
Code: Select all
Swap a(n), a(range(n,Ubound(a)))
Code: Select all
Swap a(n), a(range(n+1,Ubound(a)))
Difficult indeed, especially with integers!counting_pine wrote:How can you print 200 unique numbers between 0 and 99?!
Code: Select all
Dim MyInt() As DWORD ; tested pseudo code
For_ ct=0 To 999
; load a unique random number between 1000 and 1999 into the array
Rand(1000, 2000, MyInt(ct), unique)
Next
ArraySort MyInt() ; sort ascending
For_ ct=0 To 999
.if ct<9 || ct>=999-10
If_ Zero? Then PrintLine "..."
PrintLine Str$(ct), Tb$, Str$(MyInt(ct))
.endif
Next
Code: Select all
0 1000
1 1001
2 1002
3 1003
4 1004
5 1005
6 1006
7 1007
8 1008
...
989 1989
990 1990
991 1991
992 1992
993 1993
994 1994
995 1995
996 1996
997 1997
998 1998
999 1999
deltarho[1859] wrote:@dodicat
No big deal but if Rnd = 0 in Sub shuffle then we get Swap a(n), a(n).
Instead ofCode: Select all
Swap a(n), a(range(n,Ubound(a)))
this should do itCode: Select all
Swap a(n), a(range(n+1,Ubound(a)))
jj2007 wrote:Difficult indeed, especially with integers!
counting_pine wrote:It breaks the distribution of the shuffle if we disallow the possibility of Rnd=0
Code: Select all
-- To shuffle an array a of n elements (indices 1..n):
for i from 1 to n-1 do
j ? random integer such that i+1 = j < n
exchange a[i] and a[j]
There are of course infinitely many unique "numbers" between 0 and 99:)
Code: Select all
const N = 3
function factorial(nn as integer) as integer
if nn <= 1 then return 1
return nn * factorial(nn-1)
end function
dim as integer i, rndn, r
dim as integer a(0 to N-1)
print "Self-swaps allowed:"
'' iterate through all "random" possibilities
for rndn = 0 to factorial(N)-1
r = rndn
for i = 0 to N-1: a(i) = i: next i
for i as integer = 0 to N-2
swap a(i), a(i + (r mod (N-i)))
r \= (N-i)
next i
for i = 0 to N-1
print a(i);
next i
print
next rndn
print
print "Self-swaps disallowed:"
'' iterate through all "random" possibilities
for rndn = 0 to factorial(N)-1
r = rndn
for i = 0 to N-1: a(i) = i: next i
for i as integer = 0 to N-3
swap a(i), a(i+1 + (r mod (N-(i+1))))
r \= N-(i+1)
next i
for i = 0 to N-1
print a(i);
next i
print
next rndn
print
I guess I'm really not sure what is meant here by the idea of 'Rnd = 0'.
I don't see any special significance, compared to, say, 'Rnd = 1e-9'.
Code: Select all
Self-swaps allowed:
0 1 2
1 0 2
2 1 0
0 2 1
1 2 0
2 0 1
Self-swaps disallowed:
1 0 2
2 1 0
1 0 2
2 1 0
1 0 2
2 1 0
deltarho[1859] wrote:Code: Select all
Self-swaps allowed:
0 1 2
1 0 2
2 1 0
0 2 1
1 2 0
2 0 1
Self-swaps disallowed:
1 0 2
2 1 0
1 0 2
2 1 0
1 0 2
2 1 0
In the first case we have all possible permutations and each column has 2x0,2x1 and 2x2. I do not understand what the second case is telling me.
counting_pine wrote:but also shows that only two permutations of six are attainable.
counting_pine wrote:What are you deliberating over at this point?
crucial for the shuffle algorithm to be able to reach all permutations
it's impossible for the first element to appear in its original position
Users browsing this forum: MSN [Bot] and 18 guests