php - random script don't run -


php random script running slowly

here seems problem

<?php       function sortear($previtems, $number) {      ($k=0; $k<sizeof($previtems); $k++) {        if ($previtems[$k] == $number)           return true;      }        return false;    }    ($i=0; $i<=9; $i++) {              $num[$i] = rand($_get['inicial'],$_get['final']);             while (sortear($previtems, $num[$i])) {                    $num[$i] = rand($_get['inicial'],$_get['final']);              }            $previtems[$i] = $num[$i];     }     sort($num);  ?>  

any sugestion random? rest of code here

link

you had infinite loop; while loop never terminating because never increasing size of $previtems array.

try this:

function sortear($previtems, $number) {     ($k=0; $k<sizeof($previtems); $k++) {         if ($previtems[$k] == $number) {             return true;         }     }     return false; }  ($i=0; $i<=9; $i++) {            $num[$i] = rand($_get['inicial'],$_get['final']);           while (sortear($previtems, $num[$i])) {                   $num[$i] = rand($_get['inicial'],$_get['final']);          $previtems[$i] = $num[$i];           }        }  sort($num); 

granted, have no idea you're trying accomplish. know script broken because while loop keeps going , going...

edit:

if you're trying create randomized array contains values between start number , end number, this:

$initial = $_get['inicial']; $final = $_get['final'];  ($i=$initial; $i<=$final; $i++) {     $num[] = $i; }  shuffle($num); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -