2

I have a Individual class that has the follow code

public class Individual {

    static int DNA_lenght = 64;
    private int fitness = 0;

    private byte[] genes = new byte[DNA_lenght];

    public void initialise_individual(){

        for(int i = 0; i < DNA_lenght; i++){

            byte gene = (byte) Math.round(Math.random());
            genes[i] = gene;
        }

    }
}

how can I write a method to translate the whole value of the individual into a decimal value?

9
  • 1
    What are you trying to do really? Your code looks wrong and bizarre in many ways (usage of Math.random() for one). Why would you turn DNA data (apparently) into a single decimal value? Commented Oct 19, 2013 at 11:57
  • Im trying to write a genetic algorithm and the use of Math.random() is creating a random individual with a array of random chromosomes Commented Oct 19, 2013 at 11:59
  • If I am not wrong, you want to convert genes array to one deimal value? Commented Oct 19, 2013 at 12:05
  • Except that it doesn't. All of your bytes (=8 bits) are either 00000000 or 00000001. What you really want is a byte in the range 0-255. You should use Random.nextBytes(byte[] genes) to create your genes. (Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.) Commented Oct 19, 2013 at 12:05
  • But I want to store binary data in the byte array so this should be ok, isnt it? Commented Oct 19, 2013 at 12:07

3 Answers 3

2

You seem to want to create a random array of bytes. But what you are currently doing is generating a random array of 0 and 1 bytes (not bits). All of your bytes (=8 bits) are either 00000000 or 00000001. What you really want is a byte in the range 0-255. You should use Random.nextBytes(byte[] genes) to create your genes.

Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.)

Then you want to convert this back into a decimal. However, as soon as you have more than 4 bytes, the values will no longer fit into a 32-bit (=4 byte) integer. If you have more than 8 bytes, they won't fit into a 64-bit integer etc.

For conversion, I'd use the BigInteger class. It takes care of the conversion for you, so you don't have to deal with endianness.

So, assuming you want a random gene of 64 bits, you do this as follows:

public static void main( String args[] ){
      // create random object
      Random r = new Random();

      int numBits = 64;
      int numBytes = numBits/8;

      // create the byte array
      byte[] genes = new byte[numBytes];

      //fill it
      r.nextBytes(genes);

      //print it
      System.out.println("byte array: " + genes);

      //now convert it to a 64-bit long using BigInteger          
      long l = new BigInteger(bytes).longValue();

      //print it
      System.out.println("long: " + l);
  }      

Of course, you could also generate a random long value directly, by using:

Random r = new Random();
long longGenes = r.nextLong();

And convert that to a byte array if you really need one:

byte[] genes = new BigInteger(longGenes).toByteArray();
Sign up to request clarification or add additional context in comments.

2 Comments

This is great, what about conversion to int?
Added it using the BigInteger class. You can do the conversion byte-by-byte yourself, but then you have more code and you need to take care of endianness. That's why the easiest way is to just use BigInteger. For a 32bit array you would use BigInteger.intValue().
1

Firstly, why is length spelled lenght?

Secondly, this will fill the array with zeros, which I presume is not what you want.

    for(int i = 0; i < DNA_lenght; i++){

        byte gene = (byte) Math.round(Math.random());
        genes[i] = gene;
    }

Instead try something like:

    genes = new byte[64];
    new Random().nextBytes(genes);

Finally, you cannot uniquely convert a byte array of length 64 (so 512 bits) into a decimal value (double is only 64 bits).

Update

After reading the question's comments, I am lead to believe you want convert 64 bits into a decimal value. That is possible, and can be simply done with new Random().nextLong(), or if you insist on using the byte data type:

genes = new byte[8];
// fill with random values
new Random().nextBytes(genes);
// convert to long
long decimalValue;
for (int i = 0; i < 8; i++) {
    decimalValue <<= 8;
    decimalValue += genes[i] & 0xff;
}

Comments

0

I think BigInteger is more appropriate. There is a random generation such as constructor java.math.BigInteger.BigInteger(int numBits, Random rnd). Also there are constructor from byte array and toByteArray method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.