# random numbers

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to generate 24 random numbers in the range of 0..199 -
which is easy with mt_rand.

But I need to insure that there are no duplicates in the set.

Anyone have a suggestion for an easy way to do that without
derandomizing (ie sort and check each number against the one
above) the set ?

To make the task slightly more difficult, what I actually need is
25 numbers, 24 of which are random and do not duplicate the 25th
(assigned) number.

bill

## Re: random numbers

Use an array with 200 elements. Initialize all 200 elements to zero. For each
random number you generate, use it as an index into the array: if the
corresponding array element is zero, set it to one; if the corresponding
element is already one, then discard that number and generate another.

No problem -- initialize the array element corresponding to the assigned
number to one instead of zero, and proceed as described.

## Re: random numbers

Doug Miller wrote:

clever !

bill

## Re: random numbers

bill wrote:

Doug's answer will give you non duplicates, but will lose the random
sequencing unless you keep each number in its own array.

Personally, for 25 numbers I wouldn't even sort - I'd get a value and
use in_array() to see if it's in the arrray or not.  If not, I'd add it.
I'd repeat the process until I have 25 numbers in the array.

For the assigned number, just make it the first array value.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: random numbers

I'm sure you mean "keep the random numbers in a separate array". Having
(literally) "each number in its own array" isn't very useful. :-)

The former is what I had in mind; I was, after all, describing only a method
of preventing duplicates.

Certainly workable, but runs slower. Data set is small enough that it won't
matter, though.

## Re: random numbers

Jerry Stuckle wrote:

even more clever, and let php do the work.  I am still learning
what is available and I appreciate the pointers.

bill

## Re: random numbers

bill wrote:

keep on trying till you get 25 unique ones, and discard any duplicates
as you go.

## Re: random numbers

The Natural Philosopher wrote:

well yes, the question was the easiest way to find the duplicates.

bill

## Re: random numbers

Well, I'll give you an alternative suggestion, and you can decide whether
it's better or worse.

Your task is equivalent, I think, to shuffling a deck of cards.  So:
\$deck = range( 0, 25 );
shuffle( &\$deck );

Now you can pick elements from \$deck starting from the top and know you
won't get duplicates.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

## Re: random numbers

Except that he wants 25 random numbers in the range 0..199.

## Re: random numbers

Doug Miller schreef:

\$deck = range( 0, 199 );
shuffle( &\$deck );
get_1st_25 ( &\$deck );

??

--
Luuk

## Re: random numbers

Luuk wrote:

It turned out the problem was slightly worse.
I needed 25 random numbers in the range of 1..99 (not 199) and
the 25 numbers had to not include any that were in a 2nd array of 5.

Here is the final (+/-) code:

while (count(\$pictures) < 26) { //while
\$new_picture = mt_rand (0,99);
if ((!in_array(\$new_picture, \$pictures)) &&
(!in_array(\$new_picture, \$correct_pictures))){
\$pictures[] = \$new_picture;
}
} //while

then I had to put in one (selected randomly) of the 5 correct
pictures:

\$correct_picture = \$correct_pictures[mt_rand(0,4)]; // select one
correct picture

array_splice (\$pictures, mt_rand(0,24),1,\$correct_picture);

works just fine -
Thank to all who helped.

bill

## Re: random numbers

Well that's slightly harder but what was wrong with array_rand()?
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.

## Re: random numbers

Geoff Berrow wrote:

it has been long enough that I don't remember what your
array_rand was.

bill

## Re: random numbers

array_rand — Pick one or more random entries out of an array
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.

## Re: random numbers

Geoff Berrow wrote:

seems wonderful, is this a predefined php function, if so, who's
library ?

## Re: random numbers

It's a PHP function

http://uk2.php.net/manual/en/function.array-rand.php

I use it for an easy lottery (UK) ball picker.

<?php
\$array=array_rand(array_flip(range(1,49)),6);
asort(\$array);
echo implode(", ",\$array);
?>
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.

## Re: random numbers

Geoff Berrow wrote:

Thanks Goeff,

I am an idiot.  I searched php.net for array_rand() and because
of the parens it did not pop up.  Now I found it.
Thanks

bill

## Re: random numbers

bill wrote:

Did you even bother to look to see if it is listed in the PHP documentation?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: random numbers

Jerry Stuckle wrote:

yes I did, see my prior note.
bill