Asmaa Daoud
Asmaa Daoud

Reputation: 79

How to fix the occurrence of random numbers

I have the following code:

Accuracy=off(data,z)
for i=1:100
    n = size(data,1);
    data_rand = data(randperm(n),:);
    m = ceil(n/20);
    k = 1:m:n-m;
    test = data_rand(k:k+m-1,:);
    train = [data_rand(1:k-1,:); data_rand(k+m:end,:)];
    %/  code to calculate auc
end
accuracy=mean(auc)

Every iteration the values of train and test are changed, so the value of auc is changed every time.

Say the final result is accuracy=0.7654 and another time accuracy=0.6543 or accuracy=0.4657. I want to fixed a specific result, say 0.6543, i.e if I run previous code more times I want to obtain the same result (0.6543).

Upvotes: 0

Views: 63

Answers (1)

Adriaan
Adriaan

Reputation: 18177

Your problem is the Random Number Generator (RNG). Fix this by setting rng('default') as first line after the declaration of the for loop. This forces the RNG to always start at the same point.

The seed can be chosen simply:

seed = 4; %// starts the seed at 4
rng(seed);

You can play with the number of the seed to find which number results in a satisfying permutation for you.

Upvotes: 1

Related Questions