LovecraftHP
Programmer
Hi guys!
I'm working on a little program to randomly shuffle my data (for use in tenfold cross-validation experiments). My data file has 4332 records, so I thought I'd first fill an array with numbers from 1 to 4332 (each number being in there once) and then later use these numbers as indices to shuffle my data. The program already does everything I want it to apart from the all-important fact that it doesn't give me all numbers between 1 and 4332. Instead it repeats certain numbers even though I put in a test to prevent these repetitions. I have no idea what I'm doing wrong here so any help would be greatly appreciated! Here's my code so far:
Thanks for looking!
I'm working on a little program to randomly shuffle my data (for use in tenfold cross-validation experiments). My data file has 4332 records, so I thought I'd first fill an array with numbers from 1 to 4332 (each number being in there once) and then later use these numbers as indices to shuffle my data. The program already does everything I want it to apart from the all-important fact that it doesn't give me all numbers between 1 and 4332. Instead it repeats certain numbers even though I put in a test to prevent these repetitions. I have no idea what I'm doing wrong here so any help would be greatly appreciated! Here's my code so far:
Code:
{
while (x<(NR+1)) {
number=1+int(rand()*4332)
if (number in sample) {
continue
}
else {
inst[x]=$0
sample[x]=number
x++
}
}
}
END {
for (z=1;z<=NR;z++) {
n=sample[z]
print inst[n] > "random.txt"
}
}
Thanks for looking!