Welcome Guest, Not a member yet? Register   Sign In
Loop logic is faulty somewhere
#1

[eluser]stevefink[/eluser]
Hi all,

This isn't a CI specific issue, more like a user error on my behalf. ;-) I'm almost embarrassed to ask for a second pair of eyes on something this rudimentary, however I seem to keep running into an inconvenient complication.

I'm essentially looking to assign a random 7 digit number to a column in a mysql table which is declared as the following:

`anonymous_extension` mediumint(9) default NULL,

My code looks something like this:

Code:
do {
                            
                            $num = (int) rand(1000000, 9999999);
                            
                        } while ($this->dbmodel->check_num($num));

and the check_num function:

Code:
function check_num($n)
    {
          $sql = "SELECT num
                    FROM foo
                    WHERE num = ?";
                    
        $query = $this->db->query($sql, array($e));
                
        if($query->num_rows() > 0) {
            log_message('debug', "We have a problem, houston. We need another num! ");
            return TRUE;
        } else {
            $row = $query->row();
            log_message('debug', "This num should be good to go, returning false! . {$row->anonymous_extension}");
            return FALSE;
        }
    }

A lot of the time I get the same number as per the following:

DEBUG - 2008-04-23 23:30:54 --> This num should be good to go, returning false!
ERROR - 2008-04-23 23:30:54 --> Query error: Duplicate entry '8388607' for key 2

This in turn also breaks my MySQL transactions and I need to restart MySQL which really hurts my application, but that's in another thread... the question here is, where is my logic flawed that I keep getting duplicate numbers? Am I doing something that's blatantly obvious that anyone can spot?

Thanks for the advice...

/sf
#2

[eluser]wiredesignz[/eluser]
What you need is a seed for the random generator, modify this to suit.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    function generate_uid()
    {
        // Find seconds and microseconds from system clock
        list($usec, $sec) = explode(' ',microtime());

        // Seed the random number generator with above timings
        mt_srand((float) $sec + ((float) $usec * 1000000));

        // Generate hash
        return sha1(uniqid(mt_rand(),true));
    }
#3

[eluser]mironcho[/eluser]
According to php manual "As of PHP 4.2.0, there is no need to seed the random number generator".
You could use random_string() form string helper with numeric/nozero option, or even mysql's RAND function - somethig like "SELECT FLOOR((RAND() * 99999999));".
It's up to you to check if generated number doesn't exist in db table before inserting, or add additional PK (auto increment) so the combination of both will be unique.
#4

[eluser]wiredesignz[/eluser]
Seed may not be required, but seeding with a random value is more likely to return a random value.
#5

[eluser]stevefink[/eluser]
There's a myriad of ways of generating a random number for my purposes. It doesn't have to be secure, and my code is built for anytime a duplicate is generated. The question here is why is the system not detecting when a duplicate is created... as far as a random number generator, I have a few different algorithms that all work. For these purposes they're not as important as say, a random password generator.




Theme © iAndrew 2016 - Forum software by © MyBB