0

I'm working with genetic algorithms and I need to create a population. Currently I have this function:

/// @notice Create a new individual
function createIndiv(uint _popSize) public{
    for(uint i = 0; i < _popSize; i++){
        Individual newIndiv = new Individual();
        newIndiv.createIndividual(agents.length);
        population.push(newIndiv);
    }
}

The point is... this way all my Individual objects have the same information (the same genes actually), but I have a function to create a random number:

function random(uint _interval) public returns (uint) {
    uint randomnumber = uint(keccak256(abi.encodePacked(now, msg.sender, nonce))) % _interval;
    nonce++;
    return randomnumber;
} 

If I try to create all my individuals in the same transactions, then it seems all of them will have the same information. I tried to create those individuals using different transactions (I mean, in many calls) and it worked. So, can't I create all my population (individual' objetcs) using only one transaction?

P.S: is that a good way to generate random numbers between an interval?

Thanks in advance!

  • You can pass i to function createIndividual, from there to function random, and from there to abi.encodePacked. As to your question - it is generally not true random, in the sense that one can predict it in advance. – goodvibration Nov 9 '20 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.