# random without replacement

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

•  Subject
• Author
• Posted on
Hi Folk

I want a random list of numbers between 0 and 58 where each number can only
occur once.  I have written this (\$f is an array):

for(\$i = 0; \$i < 13; \$i++) {
\$rand[\$i] = mt_rand(0,count(\$f));
}

what else do I need?

TIA

## Re: random without replacement

windandwaves wrote:

This is what I came up with:

for(\$i = 0; \$i < 12; \$i++) {
\$r[\$i] = mt_rand(0,count(\$f));
for(\$j = (\$i - 1); \$j > -1; \$j--) {
if(\$r[\$j] == \$r[\$i]) {
\$i = 0;
}
}
}
but it does not seem to work

Any suggestions?

TIA

## Re: random without replacement

a simple word of advice: Do not use variable names that are the same
with php functions. If  at any point you forget the \$, strange things
will happen. check out rand().

and now that I am on it:

// function that generates a random number that does not exists in an
array
function get_random_number(\$random_array) {
\$random_number=mt_rand(0,58);

if(is_array(\$random_array)&&in_array(\$random_number,\$random_array)) {
return get_random_number(\$random_array); // recursiveness
} else {
return \$random_number;
}
}

// fill the array
for(\$i=0; \$i<13; \$i++) {
\$random_array[\$i]=get_random_number(\$random_array);
}

// just to showcase
foreach(\$random_array as \$number) {
echo \$number.'<br>';
}

## Re: random without replacement

\$randomstuff = range(0,58);
shuffle(\$randomstuff);

print_r(\$randomstuff);

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirvi
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

## Re: random without replacement

The "shuffle" thing does the work but produces an array with 58
elements. And it works a lot faster than the recursive thing (it
matters if the function is used more than a few times in a script).

But if you need just 13 (or anything else <>58) items in the array the
recursive function is really the only sane way to go.

(of course one could "shuffle(\$randomstuff);" and then pick up the
first 13 items from the resulted array, but I really dont like doing
that)

## Re: random without replacement

Tyxod wrote:

Why not?

1. It's much clearer than the recursive code
2. It's faster than the recursive code

If course - it does use 156 more bytes than the recursive solution.  But
the parser will probably need more than this just to handle the extra
code anyway.

Sometimes saving a few bytes just isn't worth it!

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

## Re: random without replacement

When I say that "I really dont like doing that", I mean that I really
don't LIKE that! :) It has nothing to do with the semantics of it, and
I agree with any other solution that worx, but I do not enjoy coding
like that. That's just me...

## Re: random without replacement

Well, I'm different. I don't like to "reinvent the wheel" every time I code
something, if such functionality already exists in the function library, I
use it rather than rewrite the whole thing.

As for the list being 58 elements instead of just 13, it can be easily
truncated to that using array_slice(\$randomstuff,-13) after the shuffle() so
that ain't no problem either.

It's just that it took me a minute or two to come up with the range and
shuffle solution, while writing the recursive function would mean a lot more
work. I like to keep it neat and tidy, short and simple. Good old KISS and
LOVE rules apply, Keep It Simple, Stupid and Leave Out Virtually Everything.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirvi
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

## Re: random without replacement

yeap, true, but there's no fun in that...

## Re: random without replacement

php is about efficiency first, fun second :)  It's hard to have fun with a
project that has more wires than R2D2 having sex with C3P0 in a ransacked
circuit city - when it breaks, the fun quickly turns to abject horror ;)

Best practices are called best practices for a reason...

dave

## Re: random without replacement

What's so fun about re-inventing the wheel? :-\

## Re: random without replacement

Perhaps you end up with a better wheel? Less friction? More MPG?

Perhaps you end up with no wheels at all and everything slides or
hovers along?

There's a lot of fun in reinventing the wheel. And an opportunity to
make something better.

Wheels work with friction. Lessen the friction and you end up with a
better wheel... and perhaps no braking mechanism! That in turn leads
to more revenue because the brakes require reinvention.

Jim Carlock
Reinvent the car.
Less than 3 years of oil left inside the United States Oil Reserves.
Post replies to the group.

## Re: random without replacement

On Wed, 15 Mar 2006 23:05:09 +1300, windandwaves wrote:

You should use shuffle() function. Are you trying to win powerball?

--
http://www.mgogala.com

## Re: random without replacement

Not sure about the powerball thing.  But this is how I did it in the end:

function randomizer(\$array, \$itemsneeded) {
\$r = array_rand(\$array, \$itemsneeded);
foreach(\$r as \$c) {
\$v[\$c] = \$array[\$c];
}
return \$v;
}

## Re: random without replacement

contained the following:

Here is my lottery ball picker

<?php
asort(\$rand_keys = array_rand(array_fill(1, 49,''), 6));
print "Your Lotto Numbers are: <big>".implode(\$rand_keys,", ")."</big>";
?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/