Hi, if you could use only one forever for arrays, would you choose to use mt_rand or array_rand… I know it depends on what you’re doing, but if you could use for arrays for the best random item, and couldn’t use the other, which one would you choose? Even if it means you might have to use a little more code, which one would you use. Please let me know I can’t decide which one I’d rather use for a fix on something. I’m thinking mt_rand, since I’ve heard it’s supposed to be better for arrays than the other, but I’m not positive if that is true after reading about them. Why I am asking you that have used them a lot, which one you would choose if could only choose one forever, for a better random item. Thank you.
First I would have to find what does “one forever” means and then I’ll decide which one to use.
But between mt_rand and array_rand, if you just need to pick random item from array, I am not sure how to use mt_rand if array has names keys. For numeric keys it mt_rand will work and may be a better source of randomness, but if you have named keys, I am not sure how you would use mt_rand, I mean you can do array_values() first, then select random item. It’s hard to tell because I don’t know what source of randomness php uses for array_rand. Maybe someone could examin the php source code for that, it would be a good start because it may turn out that array_rand uses the same function as mt_rand() anyway.
It doesn’t matter which one you use, both are essencially the same exact thing.
I can’t make the assumption of ‘same exact thing’ without examining the php source code first, but it’s safe to say both will produce the decent source of randomness.
Sometimes for the purpose of unit testing it’s better to use mt_rand() or rand() where you can manually provide the ‘seed’ of randomness. Then during testing as long as you keep giving it the same ‘seed’ it will always generate the same result. Otherwise it’s hard to test code when you don’t know which number will be generated each time you run the test, thus it’s impossible to assert true.
This is a little trick I’ve learned when I was learning Java. Not sure if it’s true for php, but it probably is. YOu can use srand() and provide the seed.
Yes in PHP you can also seed the RNG if you like.
However, even though it’s nice to use when unit testing I would also run at least a few rounds of unit testing where I didn’t seed the RNG.
Just to make sure my code also works when I really get a random value and not the value I got every time because I was seeding the RNG. Can’t think of a concrete example right now but it could happen
You guys you really surprised me. I really thought most of you would really be leaning one way or the other.
$one[1] = “”;
$one[2] = “”;
$one[3] = “”;
etc.
$a = mt_rand(1, count($one));
$get = $one[$a];
I guess I’ll go for mt_rand then even though takes extra step in something like this.
Thanks very much for telling me your opinions on it.
and if you would like to add even more randomness, you could shuffle() the array first
That gives it even more randomness? Didn’t know that thought random was random, thanks.
not necessarily :inspector:
just out of curiosity I have whipped up a bit of code to test the randomness of javascript’s Math.random()
it appears the higher the number of generated numbers, the more random and evenly distributed the numbers are.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
var minNum = 1;
var maxNum = 20;
var numSamples = 60;
var randNum;
var samplesCount = new Array();
//initialise samplesCount
for(var i=minNum; i <= maxNum; i++) {
samplesCount[i] = 0;
}
//generate and store the randum number between minNum and maxNum and their counts
for(var i=1; i <= numSamples; i++) {
randNum = Math.floor((Math.random()*(maxNum-minNum+1))+minNum);
samplesCount[randNum] += 1;
}
//output the results
var strResults = '';
for(var i in samplesCount) {
strResults += i + ' = '+samplesCount[i]+"\
";
}
alert(strResults);
</script>
</head>
<body>
</body>
</html>
Can you actually add more randomness ?
I’d stick with array_rand to pick a random array element, that’s what it’s for. The core PHP developers are tasked with making sure it returns a random array key. If it can be improved, or has a flaw, when doing so it will almost certainly be addressed.
Not so with picking an array key with mt_rand using your own implementation.
Wow thanks Kalon that is interesting how that works the more numbers, and Anthony that was a real revelation. What about everything saying mt_rand is supposed to be so much more random and faster too than regular rand. Seems like the would make it a tad bit faster or more than array_rand. I don’t have your guys programming talent to be able to test between all of them myself right, why I asked just what your thoughts on it.
I’m not a mathematician but from the link I posted a computer based RNG at best creates a pseudo random number using a formula and the quick and simple Math.random() test code I posted suggests that a lower number of generated random numbers for a given range will result in less evenly distributed results than a large number of generated numbers.
Googling RNG’s also shows that some RNG algorithms generate numbers that are more “random” than other RNGs.
I would expect the javascript and php RNGs use the “better” algorithms.
so using shuffle() can only increase the “randomness” of the final selection imho, but by how much I would need to ask a mathematician but I suspect it wouldn’t be by much.
Yup. You stubled upon the Central limit theorem.
The correct term is entropy
ok thanks
[ot]
Ooo shiny, will have to take a peek. Thanks![/ot]
So are you suggesting I google mt_rand RNG’S? I did but didn’t find concrete results about that. You were probably talking about RNG’S in general, it was interesting.