Output near random values in PHP


Nice to meet you. My name is Nishikawa, and I joined the company in June.
In this article, I will show you how to create a random number in PHP.
A random number is one that has no artifice in the sense that it is generally a value created "at random" without any law.
For those who use PHP regularly, the mt_rand() function, which is called a pseudo-random function, is familiar.

This function returns a random value depending on when it is executed.
It is only omitted in the processing, but the seed value is set internally.
The seed value is automatically set depending on the timing of processing, but you can keep returning the same value by fixing the seed value with mt_srand().
This is the reason why it is called pseudo-random.

As mentioned above, seed value is automatically assigned in php4.2 or later, so it is random.
It is very useful. First, we will describe the generation of a commonly used random number.


Use mt_rand(), which we told you about at the beginning.


The HTML part above is provided to clarify the action of the person.

"The time between when the content is displayed and when the button is pressed is how people behave.
Some actions can further increase the randomness.
We will create a random number based on the data sent from the HTML above.


Execute the ajax in the aforementioned HTML.
A random number is generated.

By using this kind of processing, you can get more variation in the data than when using only mt_rand() to get random numbers. In actual implementation, it is possible to hide the seed value by processing it internally with php.

The above implementation depends on the seed value and is not completely random, so it is not suitable for cryptographic processing.