Jump to content

Random number


sudeepk

Recommended Posts

Hi, I need to generate random numbers which i have a limit of min 1 and max 7000. And in between this numbers i don't need to introduce few sets of numbers like 400 to 800 and 4600 to 5000. Please guide me.

 

I simply wrote

 

$rand = (rand(1,7000));

 

Now, how should i stop those numbers from coming up.

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/
Share on other sites

Try:

<?php
    function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom] == $elem)
                return true;
            else
                if(is_array($array[$bottom]))
                    if(in_multiarray($elem, ($array[$bottom])))
                        return true;
                   
            $bottom++;
        }       
        return false;
    }

$ranges = range(4600,5000);
$ranges[] = range(400,800);
$rand = rand(1,7000);
while	(in_multiarray($rand,$ranges))
$rand = rand(1,7000);
echo "yay:".$rand;
?>

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/#findComment-1047983
Share on other sites

Dont use rand(), instead use arrays and array_rand.

$array1=array(range(1,4),range(10,20),range(50,58));
$nums=array_merge($array1[0],$array1[1],$array1[2]);
$key = array_rand($nums, 1);
echo $nums[$key];
echo "<br><pre>";
print_r($nums);

Another way would be to use one array for the excluded ranges and another for the entire range and do an array_unique then an array_rand on the resulting array.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/#findComment-1048006
Share on other sites

Not saying my way is the best possible way to do it, but it works.

<?php
    function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom] == $elem)
                return true;
            else
                if(is_array($array[$bottom]))
                    if(in_multiarray($elem, ($array[$bottom])))
                        return true;
                   
            $bottom++;
        }       
        return false;
    }
   
$ranges = range(4600,5000);
$ranges[] = range(400,800);
$ranges[] = array(46, 207, 336, 982);
$rand = rand(1,7000);
while   (in_multiarray($rand,$ranges))
$rand = rand(1,7000);
echo "yay:".$rand;
?>

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/#findComment-1048107
Share on other sites

It works like a charm. :)

 

I have a small problem again and a last one.

 

Can i implement something like if the value of the input is zero or any rejected number by brute forcing like

 

project.php?input=4275

 

where 4275 is already banned.

 

So that it redirects to the home page

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/#findComment-1048140
Share on other sites

Here i start with index page with link which generates a random link in which it has a "random number".html iframed. So finally the link looks like project.php?input=stuff/4214.html . In this link when i delete the ?input=stuff/4214.html leaving project.php as link. The iframe shows the parent website in the iframe. So here i need to do some coding where if i open project.php directly it redirects to index.php where people can start with the generated link as usual.

 

I read a php ebook today and started working. Iam a noob in php. :(

Link to comment
https://forums.phpfreaks.com/topic/199673-random-number/#findComment-1048153
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.