Random numbers in arrays
Author Message
Random numbers in arrays

I'm needing some help with random numbers, which I'm hoping someone here
can assist with.

I've got a one-dimension array of size n.  n will be known in advance
and won't change very often - a few times a year at most.  n will be
around 5 and is unlikely to grow above 20.

I'm trying to populate this array with the integers 0 to (n-1) (or 1 to
n - I'm not bothered which).  The complication is that I want this
population to be random (or rather pseudo-random - it doesn't need to be
genuinely random).

Thus if n=3, one visitor to the page might find the values were 0,1,2
while another might find them to be 1,2,0 and so on.

So far, I've got the following (where n is 3):

\$randsections[0]=rand(0,2);

\$randsections[1]=rand(0,2);
while (\$randsections[0]=\$randsections[1])
{ \$randsections[1]=rand(0,2); }

\$randsections[2]=rand(0,2);
while (\$randsections[2]=\$randsections[0] or \$randsections[2]=\$randsections[1])
{ \$randsections[2]=rand(0,2); }

This has the following problems:

1. The code's somewhat messy, and if n did get to 10 or something, would
become very messy.
2. The while loops are not very efficient and could be never-ending if
the random number chosen kept on being one that had already been chosen.
(Again, this would be worse if n got to 10 or so.)
3. It doesn't work!  Printing the values at the end gives them as 0,0,0
(though printing the value of [0] immediately after assigning it works,
so it's not a problem with random number generation).

Can anyone give me a few pointers as to what I should be doing
differently?  I suspect there's a much neater way of doing this, but I
couldn't find anything on php.net (though I didn't really know where to
begin looking).  I'm very new to PHP - I've done the Webmonkey course
and that's about it - so any help would be appreciated.

For those who are interested as to why I'm doing this: I've got a page -
www.lavenham.co.uk/accommodation - with various hotels and B&Bs in
Lavenham.  At the moment, it's a static page so the place listed first
gets much more business as a result than the place listed last.  Given
that they all pay the same to be on there, I want to display the places
in a random order so they get equal prominence.

Regards,
Stuart.

--
Stuart Thomson.

Wed, 27 Apr 2005 02:36:13 GMT
Random numbers in arrays

Quote:

>I've got a one-dimension array of size n.  n will be known in advance
>and won't change very often - a few times a year at most.  n will be
>around 5 and is unlikely to grow above 20.

>I'm trying to populate this array with the integers 0 to (n-1) (or 1 to
>n - I'm not bothered which).  The complication is that I want this
>population to be random (or rather pseudo-random - it doesn't need to be
>genuinely random).

>Thus if n=3, one visitor to the page might find the values were 0,1,2
>while another might find them to be 1,2,0 and so on.

One approach would be to populate your array with range() to get 0..n, then
shuffle() it.

http://www.php.net/manual/en/function.range.php
http://www.php.net/manual/en/function.shuffle.php

--

http://www.andyhsoftware.co.uk/space | disk usage analysis tool

Wed, 27 Apr 2005 04:01:00 GMT
Random numbers in arrays
1. I'm shure you wanted to write
while (\$randsections[0]==\$randsections[1])
(DOUBLE = !!!)

2. Here is one idea (NOT TESTED, but may be it helps):
// Create an array:
\$usednumbers = array();
for (\$i=0; \$i<3; \$i++) {
while (in_array(\$randsections[\$i]=rand(0,2), \$usednumbers));
\$usednumbers[] = \$randsections[\$i];

Quote:
};

What it does:
- create random number and allocate it to the randsection
- check is this number already exists in array \$usednumbergs
- if it exists, so last two steps again (and again, and again)
- as soon as a new number is found: allocate its value to
the array of used numbers

But be carefull, if your n (in the above example 3) is very
high. It may take very much time to find an unused number
by the random process. You need to do optimization!
(But up to the requested 20 should be no problem).

Thomas

Quote:

>I'm needing some help with random numbers, which I'm hoping someone here
>can assist with.

>I've got a one-dimension array of size n.  n will be known in advance
>and won't change very often - a few times a year at most.  n will be
>around 5 and is unlikely to grow above 20.

>I'm trying to populate this array with the integers 0 to (n-1) (or 1 to
>n - I'm not bothered which).  The complication is that I want this
>population to be random (or rather pseudo-random - it doesn't need to be
>genuinely random).

>Thus if n=3, one visitor to the page might find the values were 0,1,2
>while another might find them to be 1,2,0 and so on.

>So far, I've got the following (where n is 3):

>\$randsections[0]=rand(0,2);

>\$randsections[1]=rand(0,2);
>while (\$randsections[0]=\$randsections[1])
>{ \$randsections[1]=rand(0,2); }

>\$randsections[2]=rand(0,2);
>while (\$randsections[2]=\$randsections[0] or \$randsections[2]=\$randsections[1])
>{ \$randsections[2]=rand(0,2); }

>This has the following problems:

>1. The code's somewhat messy, and if n did get to 10 or something, would
>become very messy.
>2. The while loops are not very efficient and could be never-ending if
>the random number chosen kept on being one that had already been chosen.
>(Again, this would be worse if n got to 10 or so.)
>3. It doesn't work!  Printing the values at the end gives them as 0,0,0
>(though printing the value of [0] immediately after assigning it works,
>so it's not a problem with random number generation).

>Can anyone give me a few pointers as to what I should be doing
>differently?  I suspect there's a much neater way of doing this, but I
>couldn't find anything on php.net (though I didn't really know where to
>begin looking).  I'm very new to PHP - I've done the Webmonkey course
>and that's about it - so any help would be appreciated.

>For those who are interested as to why I'm doing this: I've got a page -
>www.lavenham.co.uk/accommodation - with various hotels and B&Bs in
>Lavenham.  At the moment, it's a static page so the place listed first
>gets much more business as a result than the place listed last.  Given
>that they all pay the same to be on there, I want to display the places
>in a random order so they get equal prominence.

>Regards,
>Stuart.

>--
>Stuart Thomson.

Wed, 27 Apr 2005 04:13:51 GMT
Random numbers in arrays

Quote:
> I've got a one-dimension array of size n.  n will be known in advance
> and won't change very often - a few times a year at most.  n will be
> around 5 and is unlikely to grow above 20.

> I'm trying to populate this array with the integers 0 to (n-1) (or 1 to
> n - I'm not bothered which).  The complication is that I want this
> population to be random (or rather pseudo-random - it doesn't need to be
> genuinely random).

> Thus if n=3, one visitor to the page might find the values were 0,1,2
> while another might find them to be 1,2,0 and so on.

One way to do it would be this simple algorythm

\$n = 5;

\$my_arr = array();
for (\$i=0; \$i<\$n; \$i++) {
do {
\$var = rand(0, \$n-1);
} while (in_array(\$var, \$my_arr));
array_push (\$my_arr, \$var);

Quote:
}

Although, I like Andy's array shuffle idea better.

regards,
reggie.

Wed, 27 Apr 2005 04:36:57 GMT
Random numbers in arrays

Quote:
>  One approach would be to populate your array with range() to get 0..n,
then
> shuffle() it.

> http://www.php.net/manual/en/function.range.php
> http://www.php.net/manual/en/function.shuffle.php

This seems like the cleanest approach to me, but make sure you test it on
your system.  Most older versions of PHP have a shuffle that is not very
random, tending to leave the array elements in or near their starting

You might be able to overcome the ineffective shuffle problem by running
shuffle multiple times. If you have a version where shuffle does nothing
then obviously you need to reinstall or not use it.

Cheers,

Luke Welling
--
PHP and MySQL Web Development
by Luke Welling and Laura Thomson
http://www.amazon.com/exec/obidos/ASIN/0672317842/tangledwebdesign

Thu, 28 Apr 2005 05:40:25 GMT
Random numbers in arrays

Quote:
> >  One approach would be to populate your array with range() to get 0..n,
> then
> > shuffle() it.

> > http://www.php.net/manual/en/function.range.php
> > http://www.php.net/manual/en/function.shuffle.php

> This seems like the cleanest approach to me, but make sure you test it on
> your system.  Most older versions of PHP have a shuffle that is not very
> random, tending to leave the array elements in or near their starting

> You might be able to overcome the ineffective shuffle problem by running
> shuffle multiple times. If you have a version where shuffle does nothing
> then obviously you need to reinstall or not use it.

Also, be aware that shuffle needs to be seeded before it is run
http://www.php.net/manual/en/function.shuffle.php

From the example at the above url:
\$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle (\$numbers);

regards,
reggie.

Thu, 28 Apr 2005 06:03:59 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages