Random function [message #180544] |
Mon, 25 February 2013 20:48 |
Sonnich Jensen
Messages: 4 Registered: February 2012
Karma: 0
|
Junior Member |
|
|
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
|
|
|
Re: Random function [message #180545 is a reply to message #180544] |
Mon, 25 February 2013 20:57 |
Salvatore
Messages: 38 Registered: September 2012
Karma: 0
|
Member |
|
|
On 2013-02-25, sonnichjensen(at)gmail(dot)com <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?
Try mt_rand().
--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
|
|
|
Re: Random function [message #180546 is a reply to message #180545] |
Mon, 25 February 2013 21:13 |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 25-02-2013 21:57, Salvatore wrote:
> On 2013-02-25, sonnichjensen(at)gmail(dot)com <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?
>
> Try mt_rand().
>
file: rand.php
<?php
for ($x=1; $x<=180800; $x++) {
echo mt_rand(1,1808)."\n";
}
?>
bash:
$ php -f rand.php | sort | uniq -c | awk 'BEGIN {m=9999; r=0; }{ if
($1<m) { m=$1; }; if ($1>r) { r=$1; }} END{ print m,r; }'
72 134
This means that some number only was picked 72 times,
and another number was picked 134 times......
$ php -version
PHP 5.3.8 (cli)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
|
|
|
Re: Random function [message #180547 is a reply to message #180544] |
Mon, 25 February 2013 21:39 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
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
==================
|
|
|
Re: Random function [message #180548 is a reply to message #180546] |
Mon, 25 February 2013 21:42 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 2/25/2013 4:13 PM, Luuk wrote:
> On 25-02-2013 21:57, Salvatore wrote:
>> On 2013-02-25, sonnichjensen(at)gmail(dot)com <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?
>>
>> Try mt_rand().
>>
>
>
> file: rand.php
> <?php
> for ($x=1; $x<=180800; $x++) {
> echo mt_rand(1,1808)."\n";
> }
> ?>
>
> bash:
> $ php -f rand.php | sort | uniq -c | awk 'BEGIN {m=9999; r=0; }{ if
> ($1<m) { m=$1; }; if ($1>r) { r=$1; }} END{ print m,r; }'
> 72 134
>
> This means that some number only was picked 72 times,
> and another number was picked 134 times......
>
>
That's pretty much within statistical variance. Remember, it does NOT
mean you'll get an even distribution - in fact if you did, by definition
the results wouldn't be random.
The key is - what happens if you do it multiple times? Does the same
number show a similar pattern?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Random function [message #180549 is a reply to message #180544] |
Mon, 25 February 2013 22:11 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 25/02/13 20:48, 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
>
seed it from the clock - or use the last few significant bits of that.
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|
Re: Random function [message #180550 is a reply to message #180546] |
Mon, 25 February 2013 22:12 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 25/02/13 21:13, Luuk wrote:
> On 25-02-2013 21:57, Salvatore wrote:
>> On 2013-02-25, sonnichjensen(at)gmail(dot)com <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?
>>
>> Try mt_rand().
>>
>
>
> file: rand.php
> <?php
> for ($x=1; $x<=180800; $x++) {
> echo mt_rand(1,1808)."\n";
> }
> ?>
>
> bash:
> $ php -f rand.php | sort | uniq -c | awk 'BEGIN {m=9999; r=0; }{ if
> ($1<m) { m=$1; }; if ($1>r) { r=$1; }} END{ print m,r; }'
> 72 134
>
> This means that some number only was picked 72 times,
> and another number was picked 134 times......
>
what else do you expoect from a random generator?
>
> $ php -version
> PHP 5.3.8 (cli)
> Copyright (c) 1997-2011 The PHP Group
> Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
>
>
>
--
Ineptocracy
(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
|
|
|
Re: Random function [message #180551 is a reply to message #180544] |
Mon, 25 February 2013 22:42 |
Salvatore
Messages: 38 Registered: September 2012
Karma: 0
|
Member |
|
|
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().
--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++
|
|
|
Re: Random function [message #180554 is a reply to message #180546] |
Tue, 26 February 2013 01:46 |
Robert Heller
Messages: 60 Registered: December 2010
Karma: 0
|
Member |
|
|
At Mon, 25 Feb 2013 22:13:04 +0100 Luuk <luuk(at)invalid(dot)lan> wrote:
>
> On 25-02-2013 21:57, Salvatore wrote:
>> On 2013-02-25, sonnichjensen(at)gmail(dot)com <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?
>>
>> Try mt_rand().
>>
>
>
> file: rand.php
> <?php
> for ($x=1; $x<=180800; $x++) {
> echo mt_rand(1,1808)."\n";
> }
> ?>
>
> bash:
> $ php -f rand.php | sort | uniq -c | awk 'BEGIN {m=9999; r=0; }{ if
> ($1<m) { m=$1; }; if ($1>r) { r=$1; }} END{ print m,r; }'
> 72 134
>
> This means that some number only was picked 72 times,
> and another number was picked 134 times......
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
(72+134)/2
103
The average of just those two data points is 103, and given that the
number of samples generated was 100 times the range of values, this is
reasonable.
Note:
sh# php -f rand.php | sort | uniq -c | wc -l
1808
All 1808 possible values were hit. From an eyeball look at the counts,
I'd guess that average hit count would be about 100, which suggests a
reasonable distrubution.
It is important to know: properly functioning computers are never truely
random. These random number generators are called 'psuedo' random
number generators for a reason. The sort of generators used for common
programming language run-time libraries are not to be considered
anything close to the ballpark of the sort used by secure encryption
software (eg OpenSSL, ssh, PGP, GPG, etc.). They are generally good
enough for things like computer games.
>
>
> $ php -version
> PHP 5.3.8 (cli)
> Copyright (c) 1997-2011 The PHP Group
> Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
>
>
>
>
--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
|
|
|
Re: Random function [message #180556 is a reply to message #180551] |
Tue, 26 February 2013 10:18 |
M. Strobel
Messages: 386 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
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.
You would never get anywhere near the current state of art with a self written solution.
/Str.
|
|
|
Re: Random function [message #180558 is a reply to message #180556] |
Tue, 26 February 2013 13:52 |
Paul Herber
Messages: 26 Registered: February 2011
Karma: 0
|
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
|
|
|
Re: Random function [message #180574 is a reply to message #180551] |
Wed, 27 February 2013 11:17 |
Markus Grob
Messages: 9 Registered: September 2011
Karma: 0
|
Junior Member |
|
|
Salvatore schrieb:
> 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( /* ... */ );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
....
Sorry, but this line is the problem, which every pseudo random generator
has. What will you fill in here. What is really different. Do you have
anything, that's changing in an order, you couldn't tell?
If you choose the time, then you have a changing startvalue, but the
calculating is always the same, so that you can say, what the results
will be.
If you choose keyboardinputs, then you may have no input so you will
have an empty seed.
You can mix it (like every pseudo random generator does) and try to
calculate in a way, that a user can't tell, what the result will be, but
you will never have a random seed, so you will never have a random
calculation.
You can try to choose chaotic elements, but you have to fill them in
your array without loosing the chaotic element ;-)
Greetings from Switzerland, Markus
|
|
|
Re: Random function [message #180577 is a reply to message #180574] |
Wed, 27 February 2013 23:22 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Markus Grob wrote:
> Salvatore schrieb:
>> 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( /* ... */ );
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> ...
>
> Sorry, but this line is the problem, which every pseudo random generator
> has. What will you fill in here. What is really different.
That is *irrelevant*. The items *there* do not need to be different and can
even be ordered. It is just the data base for subsequent (pseudo-)random
selection; it suffices that the *key* for accessing that array is a
(pseudo-)random number, and if key repetitions are to be avoided, that you
remember which keys have been used.
> Do you have anything, that's changing in an order, you couldn't tell?
> […]
Sorry, you are posting in gibberish even for a native speaker of German who
could, in theory, try to guess what you meant in English by translating that
word-by-word back to German. But in practice, with what you posted this one
cannot. Therefore, I strongly suggest you stop posting in English until you
improve your English so as you need not rely only on online translators.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
|
|
|