Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8478 > unrolled thread

Random function

Started bysonnichjensen@gmail.com
First post2013-02-25 12:48 -0800
Last post2013-02-28 00:22 +0100
Articles 13 — 10 participants

Back to article view | Back to comp.lang.php


Contents

  Random function sonnichjensen@gmail.com - 2013-02-25 12:48 -0800
    Re: Random function Salvatore <sal@yojimbo.hack.invalid> - 2013-02-25 20:57 +0000
      Re: Random function Luuk <luuk@invalid.lan> - 2013-02-25 22:13 +0100
        Re: Random function Jerry Stuckle <jstucklex@attglobal.net> - 2013-02-25 16:42 -0500
        Re: Random function The Natural Philosopher <tnp@invalid.invalid> - 2013-02-25 22:12 +0000
        Re: Random function Robert Heller <heller@deepsoft.com> - 2013-02-25 19:46 -0600
    Re: Random function Jerry Stuckle <jstucklex@attglobal.net> - 2013-02-25 16:39 -0500
    Re: Random function The Natural Philosopher <tnp@invalid.invalid> - 2013-02-25 22:11 +0000
    Re: Random function Salvatore <sal@yojimbo.hack.invalid> - 2013-02-25 22:42 +0000
      Re: Random function "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-02-26 11:18 +0100
        Re: Random function Paul Herber <paul@pherber.com> - 2013-02-26 13:52 +0000
      Re: Random function Markus Grob <snoopy@ilnet.ch> - 2013-02-27 12:17 +0100
        Re: Random function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-28 00:22 +0100

#8478 — Random function

Fromsonnichjensen@gmail.com
Date2013-02-25 12:48 -0800
SubjectRandom function
Message-ID<3bbc3c88-ba02-4c72-843a-b87b01bc7c6b@googlegroups.com>
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

[toc] | [next] | [standalone]


#8479

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-02-25 20:57 +0000
Message-ID<kggj8h$sch$1@yojimbo.hack>
In reply to#8478
On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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++

[toc] | [prev] | [next] | [standalone]


#8480

FromLuuk <luuk@invalid.lan>
Date2013-02-25 22:13 +0100
Message-ID<vn0tv9-cj4.ln1@luuk.invalid.lan>
In reply to#8479
On 25-02-2013 21:57, Salvatore wrote:
> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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


[toc] | [prev] | [next] | [standalone]


#8482

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-02-25 16:42 -0500
Message-ID<kgglpj$8or$2@dont-email.me>
In reply to#8480
On 2/25/2013 4:13 PM, Luuk wrote:
> On 25-02-2013 21:57, Salvatore wrote:
>> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8484

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-02-25 22:12 +0000
Message-ID<kggnjs$b5b$2@news.albasani.net>
In reply to#8480
On 25/02/13 21:13, Luuk wrote:
> On 25-02-2013 21:57, Salvatore wrote:
>> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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.

[toc] | [prev] | [next] | [standalone]


#8488

FromRobert Heller <heller@deepsoft.com>
Date2013-02-25 19:46 -0600
Message-ID<9oudnT-AgalvjrHMnZ2dnUVZ_qudnZ2d@giganews.com>
In reply to#8480
At Mon, 25 Feb 2013 22:13:04 +0100 Luuk <luuk@invalid.lan> wrote:

> 
> On 25-02-2013 21:57, Salvatore wrote:
> > On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


                                                                                                         

[toc] | [prev] | [next] | [standalone]


#8481

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-02-25 16:39 -0500
Message-ID<kgglki$8or$1@dont-email.me>
In reply to#8478
On 2/25/2013 3:48 PM, sonnichjensen@gmail.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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8483

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-02-25 22:11 +0000
Message-ID<kggnhm$b5b$1@news.albasani.net>
In reply to#8478
On 25/02/13 20:48, sonnichjensen@gmail.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.

[toc] | [prev] | [next] | [standalone]


#8485

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-02-25 22:42 +0000
Message-ID<kggpci$1sgb$2@yojimbo.hack>
In reply to#8478
On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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++

[toc] | [prev] | [next] | [standalone]


#8490

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-02-26 11:18 +0100
Message-ID<ap3gf6FepajU1@mid.uni-berlin.de>
In reply to#8485
Am 25.02.2013 23:42, schrieb Salvatore:
> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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.

[toc] | [prev] | [next] | [standalone]


#8492

FromPaul Herber <paul@pherber.com>
Date2013-02-26 13:52 +0000
Message-ID<37fpi8dpka1hmn2n33anipmf48h8cbp6ab@news.eternal-september.org>
In reply to#8490
On Tue, 26 Feb 2013 11:18:13 +0100, "M. Strobel" <sorry_no_mail_here@nowhere.dee> wrote:

>Am 25.02.2013 23:42, schrieb Salvatore:
>> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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      

[toc] | [prev] | [next] | [standalone]


#8508

FromMarkus Grob <snoopy@ilnet.ch>
Date2013-02-27 12:17 +0100
Message-ID<kgkptu$b9r$1@dont-email.me>
In reply to#8485
Salvatore schrieb:
> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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

[toc] | [prev] | [next] | [standalone]


#8511

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-02-28 00:22 +0100
Message-ID<1596284.F5I4JHaoKD@PointedEars.de>
In reply to#8508
Markus Grob wrote:

> Salvatore schrieb:
>> On 2013-02-25, sonnichjensen@gmail.com <sonnichjensen@gmail.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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web