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)))
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
It breaks the distribution of the shuffle if we disallow the possibility of Rnd=0, although yes, it may be worth checking for and skipping the self-swap, to make sure nothing bad happens. (I seem to recall swapping an object with itself can have construct/destroy ordering issues?)deltarho[1859] wrote:@dodicat
No big deal but if Rnd = 0 in Sub shuffle then we get Swap a(n), a(n).
Instead ofthis should do itCode: Select all
Swap a(n), a(range(n,Ubound(a)))
Code: Select all
Swap a(n), a(range(n+1,Ubound(a)))
(And yes, I should also have said Integers. There are of course infinitely many unique "numbers" between 0 and 99:)jj2007 wrote:Difficult indeed, especially with integers!
I would agree if that was true but we are not disallowing the possibility of Rnd = 0.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]
True if "numbers" is floating point but finite if "numbers" is either Single or Double.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
There isn't. We would get Swap a(n), a(n) if Rnd was sufficiently small.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
The second case is telling you what happens if self-swaps are disallowed.deltarho[1859] wrote: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.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
I was never concerned about all possible permutations, only those which did not have self_swapping.counting_pine wrote:but also shows that only two permutations of six are attainable.
Being very analytical sees me turning over more stones looking for trouble than most people do. <smile>counting_pine wrote:What are you deliberating over at this point?
I agree but is that necessary from a shuffling perspective? If a set of numbers is sufficiently large then it will be impossible for a PRNG to reach all permutations anyway, as shown earlier.crucial for the shuffle algorithm to be able to reach all permutations
I know, but that did not concern me - I was concerned about shuffling.it's impossible for the first element to appear in its original position