Jump to content

a help


karthikeyan_coder

Recommended Posts

@Navarr,

 

the for loop should be

 

for($i = 0;$i < 10000;$i++) {

 

But if you look at the results, uniqueness criteria is not met

 

<?php
     function random_tenthou_gen() {
          $numlist = array();
          for($i = 0;$i < 10000;$i++) {
               $numlist[] = rand(1,10000);
          }
          return $numlist;
     }
     
     
     $a = random_tenthou_gen();
     
     /**
     * Analyse results
     */
     $ka = array_count_values($a);
     $kb = array_count_values($ka);
     ksort($kb);
     $tot=0;
     foreach ($kb as $occ => $count) {
        echo "$count numbers occurred $occ times<br>";
        $tot += $count*$occ;
     }
     echo "<br/>TOTAL: $tot";
?>

Link to comment
https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235119
Share on other sites

Here:

<?php
function randomArray($amount=10000) {
     $usedNumbers = array();
     for ($i = 0; $i < $amount; $i++) {
          $randomNumber = rand(1, $amount);
          while (in_array($randomNumber, $usedNumbers)) $randomNumber = rand(1, $amount);
          $usedNumbers[] = $randomNumber;
     }
}
?>

It's going to be slow as hell trying to do 10,000 though.

Link to comment
https://forums.phpfreaks.com/topic/48089-a-help/#findComment-235301
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.