1

I need to get 50 random numbers out of range 1-100 without repeating. The current way i do is :

$array = array();
while (count($array) <= 50) {
  $temp = random_int(1,100);
  if (!in_array($temp, $array))
    $array[] = $temp;
}

However, the looping is too many because I need to generate for more than 100,000 times. Is there other ways that I can get a 50 random non-repeating numbers without looping ?

For example:

$number= range(1,100);
$array = array_slice(shuffle($number),0,50);

I can't use shuffle because it uses pseudo random number.

Is there other ways to achieve what I need, or ways that could shorten time.

azukitaro
  • 19
  • 2
  • Does this answer your question? [Fill an array with random numbers](https://stackoverflow.com/questions/20380991/fill-an-array-with-random-numbers) – Banzay Apr 10 '20 at 05:58
  • @Banzay the answer also using `for` loop which I'm trying to avoid using looping – azukitaro Apr 10 '20 at 07:03
  • 2
    There are several answers in this [question](https://stackoverflow.com/questions/5612656/generating-unique-random-numbers-within-a-range-php) – Jsowa Apr 10 '20 at 11:16
  • @Banzay the referenced question uses Java but this question is about PHP , so the code will be different. – Martin Apr 10 '20 at 11:23
  • @Martin ok. Sorry. Didn't notice. Retrackted close. – Banzay Apr 10 '20 at 11:25
  • @azukitaro why are you limited to not use loops and not use PRNG? You're pretty stuck without manually building the array (100,000 times) . What's your reasoning for this? – Martin Apr 10 '20 at 11:38
  • Does this answer your question? [Generating UNIQUE Random Numbers within a range - PHP](https://stackoverflow.com/questions/5612656/generating-unique-random-numbers-within-a-range-php) – Nico Haase Apr 10 '20 at 12:54

3 Answers3

1

pre fill a array of numbers and pick from them, and then remove it. it prevents the unnecessary random generations you have

$numbers = [];
for ($i = 1; $i <= 100; $i++) {
    $numbers[] = $i;
}

$randomNumbers = [];
for ($i = 1; $i <= 50; $i++) {
    $r = rand(0, count($numbers) - 1);
    $randomNumbers[] = $numbers[$r];
    array_splice($numbers, $r, 1);
}
Bernhard
  • 414
  • 3
  • 6
0

This would be my approach:

This gives you 50 numbers in any case, and they are defenitely different from each other. PLUS: you dont have to prefill some other array:

$start = microtime(true);
for($i = 0; $i <= 100000; $i++){
$arr = [];
  while(sizeof($arr) < 50){
          $num = rand(1, 100);
          $arr[$num] = $num;
  }

  if(array_unique($arr) !== $arr || sizeof($arr) !== 50 ){
              print("FAIL");
    }
       //print(array_unique($arr) == $arr ? "true" : "false");print("<br>");
       //print(sizeof($arr));print("<br>");
       //print_r(array_count_values ($arr));print("<br>");
       //print_r($arr);print("<br>");

}


$time_elapsed_secs = microtime(true) - $start;
print($time_elapsed_secs);print("<br>");

Running this 100000 times takes about 0.4sec for me.

The actual generation is done in this part:

$arr = [];
  while(sizeof($arr) < 50){
          $num = rand(1, 100);
          $arr[$num] = $num;
  }
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
-1

We can do in 2 steps:

$x = 0;
$arr = [];
while($x < 50){
    $tmp = rand(1, 100);
    if(!in_array($tmp, $arr)){
        $arr[] = $tmp;
        $x++;
    }

}
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
  • it will give 50 random numbers and each number will be between (1 to 100). – Naveed Ramzan Apr 10 '20 at 12:17
  • 1
    NO! it could randomly give you an array with 50 times 0 in it. (probability is low) but it could happen. just try to run your code a few times and you will see what i mean...it almost never returns an array of size 50 in the end. – DigitalJedi Apr 10 '20 at 12:28
  • I did and attached the screenshot. It works fine and I tested 5 times :) maybe you are trying to say something else. – Naveed Ramzan Apr 10 '20 at 12:44
  • run this $x = 0; $arr = []; while($x <= 50){ $arr[] = rand(1, 100); $x++; } $arr = array_unique($arr); print_r(sizeof($arr)); and tell me if the array is always length 50. I assume you ran some other code. – DigitalJedi Apr 10 '20 at 12:48
  • Well, code is same and strange here its showing up to 49 indexes means 50 elements. – Naveed Ramzan Apr 10 '20 at 12:50
  • NO! NO! NO! your indizes miss 8,22,32,34,38 and 48, bacause they were removed by the array_unique. just because the last index is 49 and the first one is 0 does not make the array an array of length 50... – DigitalJedi Apr 10 '20 at 12:53
  • OK so you want to add in_array at adding in `$arr` ? main point to answer here to give an idea. of course not 100% exact answer :) – Naveed Ramzan Apr 10 '20 at 13:05
  • the OP clearly states he wants 50 different random numbers. I see you have updated your question to inclue in_array... however your result still does not return 50 values in the cases where in_array($tmp, $arr) is true even just once...I suggest you delete your answer – DigitalJedi Apr 10 '20 at 13:21
  • Well have you checked the answer as I have updated. and `$x++` is inside that if. – Naveed Ramzan Apr 10 '20 at 13:34
  • Thanks for your guidance. now seems to be a acceptable answer if yes you can remove your down vote. Thanks – Naveed Ramzan Apr 10 '20 at 13:42
  • It's good to see that you try to improve your answer, but I cannot see how your current answer is different from the code in the question. It still lacks the same problems, and it doesn't contain any explanation that justifies why your code should do anything better than the code in the question.... – Nico Haase Apr 11 '20 at 08:57