|
Post by chad3006 on Jan 18, 2022 14:17:10 GMT -5
I'm having some issues getting a program to list some random numbers with no duplicate numbers. It's really a sorting program, I guess. The following is a stripped down version of the program which will randomize 10 numbers, but I'll eventually need to sort almost 2000 numbers. I can successfully identify the duplicates, but when I try to re-randomize it seems to still include duplicates.
[START]
cls a = 0 c = 0 qtyfiles = 10 dim check$(qtyfiles) cls [NEWARRAY]
for c = 1 to qtyfiles GOSUB [RANDOMIZER] print check$(c) next c print
[CHECKER]
for c = 1 to qtyfiles for a = 1 to qtyfiles if check$(c) = check$(a) then if c <> a then ' TO ELLIMINATE CHECKING NUMBER AGAINST ITSELF print "y"; " "; check$(c); " "; check$(a) ' GOSUB [RANDOMIZER] ' GOTO [CHECKER] else end if else print "n"; " "; check$(c); " "; check$(a) end if next a print "***********" next c
[END] ' print ' for c = 1 to qtyfiles ' print check$(c) ' next c end
[RANDOMIZER] number$ = str$(int(rnd(qtyfiles)*qtyfiles) + 1) check$(c) = number$ RETURN
|
|
|
Post by Rod on Jan 18, 2022 15:09:58 GMT -5
So do you want to shuffle a defined set of numbers. So numbers 1-100 shuffled randomly with no duplicates, just 1-100 in random order?
|
|
|
Post by chad3006 on Jan 18, 2022 15:58:25 GMT -5
The big picture is this: I need to read a directory of files, then rename them randomly with a number for their name. I've got the rename and directory reading figured out.
Yes, it would be a bit like shuffling cards...except for more than 52.
|
|
|
Post by Walt Decker on Jan 18, 2022 17:30:27 GMT -5
That is like "how long would it take 1000 monkeys to with McBeth?" Answer, never.
The very nature of randomness is to expect duplicates given a large enough sample.
What you should do is put the numbers in an array, sort the array and eliminate duplicates, then run your generator again.
|
|
|
Post by klewlis on Jan 18, 2022 19:05:22 GMT -5
you could load your numbers into an array, then randomly shuffle them, here is one such example.
qtyfiles = 100
dim filename$(qtyfiles)
for a = 1 to qtyfiles filename$(a) = str$(a) + ".dat" 'or whatever extension you need. next a
for a = 1 to 100 print a; print space$(5); print "filename$("; a; ") = "; filename$(a) next a
for a = 1 to (qtyfiles * 100) i = int(rnd(1) * qtyfiles) + 1 tempvar$ = filename$(i) j = int(rnd(1) * qtyfiles) + 1 filename$(i) = filename$(j) filename$(j) = tempvar$ next a
for a = 1 to qtyfiles print a; print space$(5); print "filename$("; a; ") = "; filename$(a) next a
wait
end
then rename your files using the array in numerical order.
|
|
|
Post by Rod on Jan 19, 2022 5:51:14 GMT -5
You need an array, a two column array. The first column is the file name the second is str$(rnd(0)) Then sort the array on column 2 now you have the array in random order so work the array from top to bottom renaming the file as you go from 1-n
Sort is well enough described in the help file let’s assume you have the file names in filename$() and n is the number of file names.
Dim a$(n,2) For i = 1 to n a$(i,1)=filename$(i) a$(i,2)=str$(rnd(0)) Next Sort a$(,1,n,2
|
|
|
Post by mknarr on Jan 19, 2022 13:34:51 GMT -5
What I do is have an array of 100 elements, generate a random number. lets say the number is 67, Check the array number 67 to see if there is a 1 in it. If there is a one, generate another number. If not,put a 1 in element #67 and use the number.
|
|