vendredi 27 juillet 2018

Get index of Shuffle Range on 2D Array php

I want to random 80*13 digits for genetic algorithm where 80 is popsize and 13 is dna size. And I trying to get index of range value on 2d array. To make a random int from 1 - 13 without duplicate where i mean 2d for 80 rows and 13 column. like this

$arr = [0][0]; //this is output will be same with the table value in row1 col1
$arr = [0][1]; //this is output will be same with the table value in row1 col2
...
$arr = [0][12]; //this is output will be same with the table value in row2 col1
$arr = [1][1]; //this is output will be same with the table value in row2 col2
..

i have a code like this.

<?php
function randomGen($min, $max) {
    $numbers = range($min, $max);
    shuffle($numbers);
    return array_slice($numbers, 0);
}
?>

<table>
    <tr>
        <th rowspan="2">Kromosom ke-</th>
        <th colspan="13">Stasiun Kerja</th>
    </tr>
    <tr>
        <?php
        for ($i=1; $i <= 13; $i++) { 
        ?>
            <th>
                <?php echo $i;?>
            </th>
        <?php
        }
        ?>
    </tr>
    <tr>
<?php
    for($i = 0; $i < 80; $i++) {
        $no = 1;
        echo "<td> v".$no."</td>";
        $arr[$i] = randomGen(1,13);
        for ($j=0; $j <= 12; $j++) {
            $arr[$j] = randomGen(1,13);
            echo "<td>";
            echo $arr[$i][$j];
            echo "</td>";
        }
            echo "</td><tr>";
            $no++;

    }
    // print_r($arr[0][0].' '); // for see the value is same or not
    ?>

when i try to printout $arr[0][0], thats value is not same with the table in row1 col1.

Any ideas?




Aucun commentaire:

Enregistrer un commentaire