I have 5 jpg, named 01.jpg, 02.jpg..... 05.jpg. I have a little script that generate a number between 1-5 and shop up the jpg associated with it.... fine... But each time i reload the page, the picture is "supposed" to be random, and so different, but only 5 image, i get the same image on reload "a lot more" that i what to.... so the question is... Hot to make sure, on the reload of the web page开发者_如何学Python the random number should be between 1-5 and NOT the same as it was the second before... so with that i will be sure to see on 5 consecutive reload the 5 picture at least !....
php of ajvascript please
here is the code :
<style type="text/css">
#photo { background-image: url(http://www.something/pano-0<?php echo mt_rand (1,5) ?>.jpg); }
</style>
note: for people that tell it's a sequence... no i like the random... at least do not choose the same
let say if #3 show, you random... and you can have 1,2,3,4,5, let get 2, if 3, re-random!
Very similar to @Jon Snyder's answer, but using $_SESSION
instead of $_GET
:
<?php
// initialize session
session_start();
// array of possible images
$images = array('01.jpg', '02.jpg', '03.jpg', '04.jpg', '05.jpg');
// check if user has seen an image before
$last = isset($_SESSION['last_img']) ? $_SESSION['last_img'] : -1;
// generate a random number that isn't the same as $last
do { $num = rand(0, count($images)-1); } while ($num == $last);
// display image
echo '<img src="' . $images[$num] . '">' . PHP_EOL;
// save last image in $_SESSION
$_SESSION['last_img'] = $num;
?>
Roll a die. Do you ever get the same number twice or even three times in a row? Do you doubt the die's randomness?
With 5 images, you have a 20% chance that the image this time will be the same as the last one. You don't want a random distribution. You just want to rotate them every time. We promise not to tell your users it's not truly random ;)
When you make the request for the next image, append a query string parameter with the id of the current picture. Then in your php code grab the value and use it to generate a random number that is not the same.
http://yourdomain/randomPicture.php?last=1
<?php
$last = $_GET['last'];
do {
$next = rand(5);
} while( $next != $last);
echo '<img src="/picture'.$next.'.jpg" />';
echo '<a href="/randomPicture.php?last='.$next.'" >next</a>';
If you really want it to be random, you will et the same number multiple times in a row - especially with such a small range of 1-5
You could specifically code to remember the last number and if the new number matches, generate again - but in reality, a random number is just that.
Ok, technically it's a pseudo-random number but for what you're dealing with it makes no difference
It might help if you can share the code that you are currently using to generate a random number. And even if you are generating truly random numbers, it is entirely possible to not see all 5 images in 5 reloads. In fact, it is highly unlikely.
Here is some pseudocode that might help:
int lastRandomNumber = -1;
function getRandom() : int {
int temp = rand();
while (temp == lastRandomNumber && lastRandomNumber != -1) {
temp = rand();
}
lastRandomNumber = temp;
return lastRandomNumber;
}
This keeps a reference to the last randomly generated number and each time you ask for a new one it will keep regenerating until it gets a new value. However, depending on your generator there is the unlikely possibility that you get stuck in a very long loop.
edit: why was this voted down?
精彩评论