php

Shortest way to generate 20 random numbers with specific range


I need to generate an array containing 20 random numbers between 1 and 200. Is there a shorter/cleaner code I can use?

<?php 
$x= array (rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200),rand(1,200));
echo '<pre>'; print_r($x); echo '</pre>';
?>

Solution

  • <?php
    $arr = array();
    for($i=0; $i<20; $i++){
      $random_num = rand(1,200);
      array_push($arr, $random_num);
    }
    
    ?>