MyBB Community Forums

Full Version: Random function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's a nifty function that comes handy every now and then. It generates X number of random numbers, ranging from A to B, but with no duplicates. PHP's rand() produces more duplicates than anything

Quote:// php
// $vinoth = number of 'numbers' to generate
// $tikitiki = start of range
// $mybb = end of range
function get_rand($vinoth, $tikitiki, $mybb) {
$rand = array();
$rand_temp = array();

while(count($rand) < $vinoth) {
$temp = rand($tikitiki, $mybb);
if(!isset($rand_temp[$temp])) {
$rand_temp[$temp] = true;
$rand[] = $temp;
}
}
return $rand;
}
lol, why did you ever feel the need to have those variable names.. haha, nice one anyways..
well just for fun, i do such things when i am free, currently i am very busy, that is why i could not devote my time to mybb.

i am thinking of some cms intergation with mybb ( planning to do ) , i am phpbb user for more than 5+ years, i know every line of the code phpbb. i am yet to look at the code of mybb.

just installed the board, not even looked at some features in mybb. no time for me.
vinoth Wrote:Here's a nifty function that comes handy every now and then. It generates X number of random numbers, ranging from A to B, but with no duplicates. PHP's rand() produces more duplicates than anything

Quote:// php
// $vinoth = number of 'numbers' to generate
// $tikitiki = start of range
// $mybb = end of range
function get_rand($vinoth, $tikitiki, $mybb) {
$rand = array();
$rand_temp = array();

while(count($rand) < $vinoth) {
$temp = rand($tikitiki, $mybb);
if(!isset($rand_temp[$temp])) {
$rand_temp[$temp] = true;
$rand[] = $temp;
}
}
return $rand;
}

Infinite loop:
$result = get_rand(10, 0, 1);
lol, nice use of variables.
haha well there would be much use in my future release over the intergation of mybb with other cms.