In PHP, How to Generate a Big Pseudo-Random Number

In PHP, how do I generate a big pseudo-random number?

Try the following:

function BigRandomNumber($min, $max) {
$difference = bcadd(bcsub($max,$min),1);
$rand_percent = bcdiv(mt_rand(), mt_getrandmax(), 8); // 0 - 1.0
return bcadd($min, bcmul($difference, $rand_percent, 8), 0);
}

The math is as following: multiply the difference between the minimum and maximum by a random percentage, and add to the minimum (with rounding to an int).

Generate BIG random number php

try this

function bigNumber() {
# prevent the first number from being 0
$output = rand(1,9);

for($i=0; $i<74; $i++) {
$output .= rand(0,9);
}

return $output;
}

PHP - Pseudo Random Number Generator?

mt_srand(42);

echo mt_rand(1, 100);
echo mt_rand(1, 100);
echo mt_rand(1, 100);

This produces the sequence 64, 80, 96 on my system. It will do so every time you execute this code. You seed the generator once with your specific number (here 42), then call the generator again and again to produce a random number.


A random number generator (truly unpredictable randomness) cannot be seeded and produces a truly unpredictable random number. Computers typically cannot do this, since randomness is precisely what they do not do. Computers are deterministic and cannot produce true randomness. You need to do something like measuring radioactive decay to produce true randomness.

Pseudo random number generators appear on the face of it to behave randomly, but they are not. They start with one number, then apply deterministic mathematical operations to that number to change it and produce a different number. Each time you call the generator, it will produce a new number based on its last number. The sequence of a PRNG is therefore always the same. Good PRNGs apply operations in such a fashion that the sequence looks very randomly distributed. Typically they're seeded with something like the time of day, so they appear random if you don't seed them explicitly. If you seed them with a specific value though, they'll produce a fixed predetermined sequence of numbers.

reproducible random number series

The Mersenne Twist is a nice fast PRNG and here's a public domain PHP implementation for it:

http://kingfisher.nfshost.com/sw/twister/

That only works on PHP 5.3.0 and above.

Generate cryptographically secure random numbers in php

Pseudorandom number generators (PRNG) are very complex beast.

There are no real "perfect" random number generators -- in fact the best that can be done from mathematical functions are pseudorandom -- they seem random enough for most intents and purposes.

In fact, performing any additional actions from a number returned by a PRNG doesn't really increase its randomness, and in fact, the number can become less random.

So, my best advice is, don't mess around with values returned from a PRNG. Use a PRNG that is good enough for the intended use, and if it isn't, then find a PRNG that can produce better results, if necessary.

And frankly, it appears that the mt_rand function uses the Mersenne twister, which is a pretty good PRNG as it is, so it's probably going to be good enough for most casual use.

However, Mersenne Twister is not designed to be used in any security contexts. See this answer for a solution to use when you need randomness to ensure security.

Edit

There was a question in the comments why performing operations on a random number can make it less random. For example, some PRNGs can return more consistent, less random numbers in different parts of the bits -- the high-end can be more random than the low-end.

Therefore, in operations where the high-end is discarded, and the low end is returned, the value can become less random than the original value returned from the PRNG.

I can't find a good explanation at the moment, but I based that from the Java documentation for the Random.nextInt(int) method, which is designed to create a fairly random value in a specified range. That method takes into account the difference in randomness of the parts of the value, so it can return a better random number compared to more naive implementations such as rand() % range.

How can I generate unique random numbers in PHP?

If you're fetching them from database, use SQL to do your job. e.g. fetching 20 random questions (without repeating):

SELECT * FROM questions ORDER BY RAND() LIMIT 20

PHP random string generator

To answer this question specifically, two problems:

  1. $randstring is not in scope when you echo it.
  2. The characters are not getting concatenated together in the loop.

Here's a code snippet with the corrections:

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

Output the random string with the call below:

// Echo the random string.
// Optionally, you can give it a desired string length.
echo generateRandomString();

Please note that this generates predictable random strings. If you want to create secure tokens, see this answer.



Related Topics



Leave a reply



Submit