1

I'm generating an array of random numbers, between 0 and 2 with this code:

    for ($j = 0; $j < 60; $j++) {
       for ($i = 0; $i < 100; $i++) {
                $value = rand(0,2);
                $DBH->query("INSERT INTO map (x, y, value) VALUES($i, $j, $value);");
    }

And i found and oddity, as you may see here, the rows are random, but they repeat:

22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121
22121000210211220022122200120200122000122121

How can avoid that?

1 Answer 1

3

You might want to explicitly seed your generator using srand, e.g. srand(time()) (note that the srand link has a better example of seeding than just using time, depends on how random you need, I suppose).

Failing that

  • You could try using mt_rand with mt_srand
  • You could always use MySQL's rand function to generate the numbers as a workaround.
Sign up to request clarification or add additional context in comments.

1 Comment

Just saw this interesting comparison on mt_rand: portfolio.technoized.com/notes/26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.