0

I created simple project where I can search for images in folders and also show random image from folder. But here is a problem sometimes when I press "Random" it repeats some of the pictures from folder for example after pressing button "random" image " 2 " appears and after pressing again button it can load the same image from folder. How to solve this? something like variable which remember last random number generated?

// sorry for english, it's bit too late for me.

Saveen
  • 4,120
  • 14
  • 38
  • 41
Zeusek ϟ
  • 3
  • 1
  • 5
    Possible duplicate of [Generating random numbers without repeats](https://stackoverflow.com/questions/17778723/generating-random-numbers-without-repeats) – James Lingham May 25 '18 at 22:14
  • What is the range of random numbers you are generating? 1-10, 1-100, 1-1000? What random function are you using? How are you pulling the images, directly via a changing `src` or using a php file and query string? – lufc May 25 '18 at 22:31
  • @user1491032 range is variable which is counting amount of files in folder and after that it saves it. #edit I used rand for generating random numbers, and yes i'm changing directly src name of image. – Zeusek ϟ May 26 '18 at 09:26
  • nothing? really? – Zeusek ϟ May 27 '18 at 00:12

1 Answers1

0

Not clear from your question whether you're suggesting that a random number generator shouldn't generate the same number twice in a row (it can, especially if the range of numbers is small), or if you just want to ensure it's always a different number. If the latter, why not:

//session_start(); if not already started
$x = random_int($min,$max);

while ($x == $_SESSION['last_rand']) {
    $x = random_int($min,$max);
}

$_SESSION['last_rand'] = $x;
lufc
  • 1,965
  • 2
  • 15
  • 19