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
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
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
Share on other sites

Now, i just got to know that i need to avoid many individual numbers too.. :|

like 46, 207, 336, 982 so on.. etc.. :|

 

i see ranges here.. how to deal with individual numbers?

 

@teamatomic

Thanks to you too..

I will learn more about it and implement it.. :)

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.