# Random function

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi

I use the rand function to get 10 items of 1808 current itemes. I tried som
e 50 times or so and I can clearly see that it has a few "faivourite number
s" which comes all the time, and some whihc I still have to see (I am talki
ng 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

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)

Try mt_rand().

--
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++\$ UBL++++\$ L+\$ W+++\$ w M++ Y++ b++

## Re: Random function

On 25-02-2013 21:57, Salvatore wrote:

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)

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

On 2/25/2013 4:13 PM, Luuk wrote:

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
==================

## Re: Random function

On 25/02/13 21:13, Luuk wrote:

what else do you expoect from a random generator?

--
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

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)

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.

--
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

## Re: Random function

On 2/25/2013 3:48 PM, sonnichjensen@gmail.com wrote:

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)

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
==================

## Re: Random function

On 25/02/13 20:48, sonnichjensen@gmail.com wrote:

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)

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

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

Am 25.02.2013 23:42, schrieb Salvatore:

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

On Tue, 26 Feb 2013 11:18:13 +0100, "M. Strobel"

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

--
Regards, Paul Herber, Sandrila Ltd.

## Re: Random function

Salvatore schrieb:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

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

Markus Grob wrote:

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.

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