FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Random function
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Random function [message #180558 is a reply to message #180556] Tue, 26 February 2013 13:52 Go to previous messageGo to previous message
Paul Herber is currently offline  Paul Herber
Messages: 26
Registered: February 2011
Karma:
Junior Member
On Tue, 26 Feb 2013 11:18:13 +0100, "M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> wrote:

> Am 25.02.2013 23:42, schrieb Salvatore:
>> On 2013-02-25, sonnichjensen(at)gmail(dot)com <sonnichjensen(at)gmail(dot)com> wrote:
>>> I use the rand function to get 10 items of 1808 current itemes.
>>
>> If you want ten random items from an array, you could try the following:
>>
>> <?php
>> // This is the big array you're pulling something out of.
>> $lotsOfItems = array( /* ... */ );
>>
>> // This is the empty array that will eventually contain the ten random
>> // items.
>> $tenRandomItems = array();
>>
>> // Generate a range of numbers between 0 and the number of items minus
>> // one.
>> $range = range(0, count($lotsOfItems) - 1);
>>
>> // Shuffle the array of numbers.
>> shuffle($range);
>>
>> while (count($tenRandomItems) < 10) {
>> $randomOffset = array_shift($range);
>> $tenRandomItems []= $lotsOfItems[$randomOffset];
>> }
>> ?>
>>
>> Using this code, you will never, ever have a duplicate item. However, it
>> will be far more computationally expensive than using rand() or
>> mt_rand().
>>
>
> Random number generation on computers is a research subject for at least 30 years.

Make that 60/70 years. I have a 1959 IBM document on the subject.



--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ twitter: @sandrilaLtd
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Request for proofreading
Next Topic: XML error when accessing a certain method of a wsdl service
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 15:20:46 GMT 2024

Total time taken to generate the page: 0.05133 seconds