1. I'm shure you wanted to write

while ($randsections[0]==$randsections[1])

instead of 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.

>Thanks in advance for your help.

>Regards,

>Stuart.

>--

>Stuart Thomson.