# Random #'s with a twist

**URL:** <https://forum.kirupa.com/t/random-s-with-a-twist/33745>\
**Category:** programming\
**Created:** [October 23, 2003, 1:25am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745 "2003-10-23T01:25:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abzoid](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@abzoid](https://forum.kirupa.com/u/abzoid)\
**Post date:** [October 23, 2003, 1:25am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/1 "2003-10-23T01:25:38Z")

</div>

I’m venturing away from my ASP comfort zone to write what, on the surface, seems like a fairly simple script in PHP.

I have a small table that is a single row of 5 thumbnail images showing featured products. I want to select these five items at random from a specified list of items. Sounds easy enough, just generate five random numbers. BUT, how do I go about making sure that I have no duplicates?

Perplexed in Texas :puzzle:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 23, 2003, 3:05pm UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/2 "2003-10-23T15:05:03Z")

</div>

OK, I found a solution. It’s not particularly pretty, but it works. It probably would have come easier if I didn’t have to concentrate so much at this point just on PHP syntax. ASP habits are hard to break. :}

```php

<?
// Initialize
$seed = (float) microtime() * 100000000;
srand($seed);

$numitems = 12;

$var1 = rand(1,$numitems);

$not_unique = true;
While ($not_unique)
{
  $var2 = rand(1,$numitems);
  if ($var2 != $var1)
    $not_unique = false;
}

$not_unique = true;
While ($not_unique)
{
  $var3 = rand(1,$numitems);
  if (($var3 != $var2) AND ($var3 != $var1))
    $not_unique = false;
}

$not_unique = true;
While ($not_unique)
{
  $var4 = rand(1,$numitems);
  if (($var4 != $var3) AND ($var4 != $var2) AND ($var4 != $var1))
    $not_unique = false;
}

$not_unique = true;
While ($not_unique)
{
  $var5 = rand(1,$numitems);
  if (($var5 != $var4) AND ($var5 != $var3) AND ($var5 != $var2) AND ($var5 != $var1))
    $not_unique = false;
}
?>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 23, 2003, 6:33pm UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/3 "2003-10-23T18:33:16Z")

</div>

```php
<?

$ar = array(1,2,3,4,5,6,7,8,9,10,11,12);

$keys = array_rand($ar, 5);

for($x=0;$x<count($keys);$x++){
    print $ar[$keys[$x]] . "<br />";
}

?>

```

that works as well…

sorry for the delay. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 1:08am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/4 "2003-10-24T01:08:32Z")

</div>

Ah, now that’s much prettier code. :}

I need to spend some more time learning to use those array functions.

Thanks :cap:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 2:05am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/5 "2003-10-24T02:05:31Z")

</div>

Nice Vash… After reading what he said… I came up with the same conclusion in my head…

Yes ab, the array functions are incredibly nice in PHP. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 2:29am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/6 "2003-10-24T02:29:12Z")

</div>

No problem. If you ever need any help just post away or send a PM. I don’t check the forums nearly as often as I used to but I still hit them up once a day or so. 🙂 always happy to help…

Playa: Yeah, I think thats the best (fastest ;)) way to go about getting the unique values… what can I say… “Great minds think alike” and sometimes you think like me… 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 3:04am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/7 "2003-10-24T03:04:26Z")

</div>

![](http://sabrinastuff.com/pix/sabrina-twist.jpg)  
[size=1]random numbers doin’ the twist[/size]

Rev ![](http://aulman.com/rev.gif)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 3:18am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/8 "2003-10-24T03:18:27Z")

</div>

Wow, that’s one hot number you got there rev 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 24, 2003, 3:37am UTC](https://forum.kirupa.com/t/random-s-with-a-twist/33745/9 "2003-10-24T03:37:43Z")

</div>

I thought she was fairly random

Rev ![](http://aulman.com/rev.gif)
