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 #180547 is a reply to message #180544] Mon, 25 February 2013 21:39 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 2/25/2013 3:48 PM, sonnichjensen(at)gmail(dot)com wrote:
> Hi
>
> I use the rand function to get 10 items of 1808 current itemes. I tried some 50 times or so and I can clearly see that it has a few "faivourite numbers" which comes all the time, and some whihc I still have to see (I am talking about large groups of items from the 1808 picture, that I still have to see)
>
> How I can get a better random function?
>
> Currently it is:
>
> $pic=rand(1, $count); // count=1808 as of now
> WBR
> Sonnich
>

Sonnich,

Well, first of all, you need to realize that ANY random number generator
is a pseudo-random number generator - it doesn't actually generate true
random numbers, but a number based on a calculation. Any such
calculation is going to have some "favorites", and repeating with the
same seed will generate the same sequence.

With this in mind, there are a couple of ways of getting "better" random
numbers. rand() isn't the greatest in the world, but I've found it to
generally be ok. You can use mt_rand() instead, which uses a different
algorithm and therefore won't show the same results. Whether it will
work in your case or not, you'll have to determine.

You could reseed the random number generator with srand() or mt_rand()
(depending on which you'll be using) on a regular (or irregular) basis,
but it really shouldn't be necessary and I don't think it will help you
that much. Worth a try, however.

Now if you're trying to pull out say 10 unique values, no random (or
pseudo random) number generator will do that - by definition, random
numbers will get duplicates. There are a couple of ways to do this: use
array_rand($array, 10) to get 10 random elements (this may still "favor"
some items). You could also shuffle($array) to randomize the order
(perhaps more than once to get a "better" random order).

Does this help?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
[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 16:53:03 GMT 2024

Total time taken to generate the page: 0.04398 seconds