Trying to generate string of 30 random numbers as a string

332 Views Asked by At

I'm making a genetic algorithm and I'm having trouble with the mutation. What I want to do is generate 30 random characters which will act as 10 states in my finite state machine (10 sets of 3 digits), the first digit of each 3 must be between 1-4, while the other 2 digits must be between 0-9.

Once I've generated my numbers I have to convert them into a 30 character string. I'd like to only mutate something like every fifth digit too.

This is what I'm currently trying:

 % mutate probability 0.2
 if (rand < 0.2)
        % pick best chromosome to mutate
            mut = combo{10};

            for t = 3:30
               mut(t) = num2str(randi([0,9]));
            end
  end

What I currently can't do is mutate only every nth digit or make sure that the first of every 3 digits is between 1-4 while keeping the others between 0-9.

In case it's helpful: combo{10} represents 110202132224154246176368198100

1

There are 1 best solutions below

5
On BEST ANSWER

Not fully understand what you by mutating the strings, but to get a string of 30 random numbers, you can simply do as follows:

str_r = sprintf('%d',  randi([0,9],1,30));
# modify selected characters.
str_r(1:3:end) =  sprintf('%d',  randi([1,4],1,10));

Example result str_r is:

299334478355384287226314472383