For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2004 > random picture









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author random picture
Lee A. Wentzel

2004-03-26, 11:12 pm

Alright, I have one more task I am trying to solve today. That is to have a
random picture show up on my website. To make it easy (or so I believe) I
made both pictures the same size. One is called 1.jpg and other is 2.jpg.

Here is the basic idea I have to do so.

<?php
mt_rand(time());
$tmp = mt_rand(1,2);
if ($tmp == 1)
{
show one picture
}
else
{
show second picture
}
?>

Now the problem is, the "show one picture" and "show two picture". The line
of html coding I thus far is:

<img src="images/1.jpg" alt="Our store" width="210" height="250"
align="right">

So, how would I have the "images/1.jpg" reflect the $tmp ??? Thank you very
much in advance.

Lee


Mike Peters

2004-03-26, 11:12 pm

On 2004-03-21, Lee A. Wentzel wrote:
> Alright, I have one more task I am trying to solve today. That is to have a
> random picture show up on my website. To make it easy (or so I believe) I
> made both pictures the same size. One is called 1.jpg and other is 2.jpg.
>
> Here is the basic idea I have to do so.
>
><?php
> mt_rand(time());
> $tmp = mt_rand(1,2);
> if ($tmp == 1)
> {
> show one picture
> }
> else
> {
> show second picture
> }
> ?>
>
> Now the problem is, the "show one picture" and "show two picture". The line
> of html coding I thus far is:
>
><img src="images/1.jpg" alt="Our store" width="210" height="250"
> align="right">
>
> So, how would I have the "images/1.jpg" reflect the $tmp ??? Thank you very
> much in advance.
>
> Lee
>

How about :
<?php
mt_rand(time());
$tmp = mt_rand(1,2);
echo '<img src="images/'. $tmp .'.jpg" alt="Our store" width="210" height="250"
align="right">';
?>

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Patrick

2004-03-26, 11:12 pm

Mike Peters wrote:
> On 2004-03-21, Lee A. Wentzel wrote:

[...]
>
> How about :
> <?php
> mt_rand(time());
> $tmp = mt_rand(1,2);
> echo '<img src="images/'. $tmp .'.jpg" alt="Our store" width="210" height="250"
> align="right">';
> ?>
>


How much better than rand() is mt_rand()? The manual says not to bother
with the mersene twister but everyone else seems to recommend it.
Mike Peters

2004-03-26, 11:12 pm

On 2004-03-21, Patrick wrote:
> Mike Peters wrote:
> [...]
>
> How much better than rand() is mt_rand()? The manual says not to bother
> with the mersene twister but everyone else seems to recommend it.


rand() uses the libc rand() function which is considered fairly poor,
as such, mt_rand() provides a much better algorithm which will result in
a better spread and is much quicker. Also on Windows, RAND_MAX, when
using rand(), is rather low at 32768, so if you expect to need larger
values you should use mt_rand().

For examples such as the above however I doubt you'll see much
difference between rand() and mt_rand().
--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com