RE: [PHP-WIN] random elements of an array
| From: | HKG) | Date: | Fri, 22 Mar 2002 13:58:10 +0000 |
| Subject: | RE: [PHP-WIN] random elements of an array | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-12728@lists.php.net to get a copy of this message | ||
You can completely randomize an array with n elements in n operations, by
using the following brute force approach:
void ScrambleIntList(int *pnElements, int nLen)
{
for (nLoop = 0, nLoop<nLen; nLopp++) {
// Pic a random element
int nRandomPic = nLen * rand();
//Swap current element with the random pic
int nTemp = pnElement[nLoop];
pnElement[nLoop] = Element[nRandomPic];
pnElement[nRandomPic] = nTemp;
}
}
/Anders
>-----Original Message-----
>From: Afan Pasalic [mailto:afan@sprout.net]
>Sent: Thursday, March 21, 2002 9:17 PM
>To: php-windows@lists.php.net
>Subject: [PHP-WIN] random elements of an array
>
>
>Hi,
>
>My problem is: if I have an array (e.g. 1, 2, 3, 4, 5, 6, 7,
>8, 9), how I can "shake" the array and get randomized order of
>the same array (e.g. 5, 8, 1, 9, 3, 4, 7, 2, 6)?
>
>Thanks for any help!
>
>
>Afan
>