Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Whats the best way to generate a random number?

For some reason it generates over 50 a lot more often then under. Is there a way to make it evenly happen or to go under more often then over.

I am using php7's random_int is that secure? I was told to seed it what is that and how do I do it?
(Apr 11, 2016 08:41 PM)CodeBinary Wrote:  For some reason it generates over 50 a lot more often then under. Is there a way to make it evenly happen or to go under more often then over.

I am using php7's random_int is that secure? I was told to seed it what is that and how do I do it?

PHP's built in rand function works quite well, and after personally running a test 1000 times, I found it to be quite accurate for random-ness. I simply charted how many times each number in my range came out, and yes some had less while others had more - but for the most part it was evenly distributed. Therefore, I like to use:
PHP Code

<?php 
rand
($min, $max); 
[Image: vfhNNbJ.gif]
I've been hearing that mr_rand() is better:

http://www.w3schools.com/php/func_math_mt_rand.asp
[Image: wOdD4yf.gif]
(Apr 11, 2016 10:17 PM)Pillows Wrote:  I've been hearing that mr_rand() is better:

http://www.w3schools.com/php/func_math_mt_rand.asp

* mt_rand() is better
rand() generate a PRNG as every other random function. You could ask the user to move his mouse and generate something related to his mouse movement if you really want something random :)
(Apr 11, 2016 10:11 PM)Team4o4 Wrote:  PHP's built in rand function works quite well, and after personally running a test 1000 times

that's literally the equivalent of saying you drove a car 5 feet and you found it rode smoothly and that the car runs quite well.

OP: read up on Mersenne Twister and libc rand().

tl;dr neither. if you need SECURE random numbers you should use a true source of randomness (atmospheric noise, an entropy key, mouse input)

/dev/random could work too in a lot of cases where you need true randomness.

PHP Code

<?php 
hexdec
(bin2hex(file_get_contents('/dev/random', false, null, -1, 4))); 
(Apr 12, 2016 07:59 AM)carbos Wrote:  * mt_rand() is better

It isn't, apparently it tends to go with more even number than uneven numbers or the other way around.
Sometimes a fail leads to the solution.
(Apr 12, 2016 09:47 AM)mothh Wrote:  It isn't, apparently it tends to go with more even number than uneven numbers or the other way around.

don't know where you've read that shit. mersenne twister is infinitely times better than the libc random.

https://en.wikipedia.org/wiki/Mersenne_Twister
Probability of getting even or odd number is basically 50%... So yeah, maybe you could get even more often than odd, this is just probability. Do it on a large scale (x > 1000000) and check the result.
(Apr 14, 2016 03:25 AM)ASDA Wrote:  don't know where you've read that shit. mersenne twister is infinitely times better than the libc random.

https://en.wikipedia.org/wiki/Mersenne_Twister

I tested it and it seems that the `rand()` function divides it more evenly than the `mt_rand()` function.
Sometimes a fail leads to the solution.
(Apr 14, 2016 01:56 PM)mothh Wrote:  I tested it and it seems that the `rand()` function divides it more evenly than the `mt_rand()` function.

How did you tested it? On which scale?
If you're on PHP7, I'd recommend random_int, I believe it's more random than other functions.

Think of the function as a dice. But with other function, one side is always more weighted, which is less random, random_int is the most balanced IMO.
...
normally i use this function

PHP Code

<?php 
function RandomString($length = 10) {
    
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
$charactersLength = strlen($characters);
    
$randomString = '';
    for (
$i = 0; $i < $length; $i++) {
        
$randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return 
$randomString;
} 
PHP does not generate something completely random. You should try something like random.org.
(Apr 15, 2016 01:10 AM)Br4v0 Wrote:  normally i use this function

PHP Code

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

1. Not what he asked for
2. http://php.net/manual/en/function.openss...-bytes.php

OP use the function mt_rand
It produces a better random value than rand and it's 4 times faster


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by KedoxApr 24, 2019 03:54 AM 9 303 Apr 24, 2019 03:54 AM
Last Post: Squito
[Javascript]  by ProckSep 28, 2018 12:51 PM 9 310 Sep 28, 2018 12:51 PM
Last Post: Prock
[PHP] by deadsh0tJan 31, 2018 06:45 AM 3 146 Jan 31, 2018 06:45 AM
Last Post: deadsh0t