Best way to store previously generated random number?

Hi all,

I’ve got a basic function which calls PHP rand() function at least several times per a web page and spits out (renders) different images based on the random number.

There’s a probability albeit rare that it will yield the same number twice in a row. I’ve seen it happen if the page gets refreshed quickly.

I can solve this by storing the previous randomly generated number and comparing it to what’s currently generating and either staying in a loop or exiting.

My problem is how is it best to store a previous value? Writing it to a file seems very old school, slow and may cause certain server Apache issues. Global variables? If so how? Any other methods?

I only need to store the last randomly generated number really but if I could store say the last three then it would only make it more unique.

Many thanks,

That would be correct if you were to call the function several times. What I had in mind was calling it once, then using the result of that function several times across the page.

i.e. call the function, set it as $images

Then, place this code through the page

<img src=“/images/<?=$images[0]?>.gif”>

(Obviously changing 0 for each subsequent image.

Thanks for that.

Correct me if I’m wrong but won’t that lose the values (number) in the array from a previous call to this function?

I call the function regularly on any page, say 5 times. It’s something like this:

normal html code
.
.
call random-number-function();
.
.
normal html code
.
.
call random-number-function();
.
.
normal html code
.
.
call random-number-function();

etc…

Since each call is a new request to the random number generator function, the latter will re-create its array variable, which will be blank and in no way hold values from a previous attempt to call the function.

Right?

I think a neat way would be if I could somehow write the previous value to memory at a fixed memory address and then basically check that address each time I call the function, then act accordingly. Not exactly global variables but essentially something very close.

Sadly I don’t think PHP is as advanced to allow writing to memory beyond that of writing to memory aka variables style?

It’s late so if I’m barking up the wrong tree above somewhere then just feel free to say it outright, :slight_smile:

Thanks.

Hmmm. I’ll have to weigh the options and try and find the balance to on one hand keep the calls simple (so I don’t have to go re-editing tens of pages) while on the other effective.

Using mt_rand() is not much better, though I hear it’s better on Linux.

Also, with a range 0 - 25 I see that regardless whether I use rand() or mt_rand() it has a habit of not generating numbers in the 20 - 25 range. The latter is very rare.

Hmmm.

Could you create a function that generates an array of random numbers to use for the page. This way, you could check that the next number generated is not already in the array.

Something like…


<?php

function gen_images($num = 1)
{
	$images = array();
	$i = 0;
	while($i < $num)
	{
		$rand = rand(1,25);
		
		while(in_array($rand, $images))
		{
			$rand = rand(1,25);
		}
		$images[$i] = $rand;
		$i ++;
	}
	return $images;
}

$images = gen_images(5);

print_r($images); // Array ( [0] => 11 [1] => 21 [2] => 25 [3] => 16 [4] => 3 )

// END OF FILE

Quick and dirty - no doubt it could be greatly improved upon, but calling gen_images(X) should provide X number of unique numbers.

I am generating a random number in the range 0 to 25 so 26 unique numbers in total.

Then, depending on what number gets generated I detect that in a select clause and display a different image on screen.

That’s the essence of what I’m doing.

At the moment it’s possible that calling the random number generator function subsequently causes it to generate the same number twice i.e. 2, 2 or 15, 15 etc.

your random number is 0-25 ?

Maybe if I understood what you are trying to do i’ll better help you out…

My random function is a bit longer

8rVWJRAoq.jpg I use Letters Numbers though…

Thanks for your comment.

Do you think the above quote is feasible even if the random number range is only say 0-25? Correct me if I’m wrong but with such a small range it would be very difficult to write a random number generate function which doesn’t yield the same number twice in a row every now and then?

From what I understand a lot depends on what the random function uses as the seed. If it’s the default (which I believe to be the number of ms since midnight) then this is about as flexible as one can get it?

honestly you don’t need to worry about random number being generated twice… You can come up with a better formula to prevent duplicate numbers.

Try random number based on time and date * rand etc… or just come up with a clever formula.

But if you feel you really need to store random number there are many ways to do it.

If you want to start more than 1 number i would recommend a database
If you’re storing 1 previous number you can use sessions or create a class file that remembers the previous number in memory.