In PHP development, we often need to generate data samples that “look random but are controllable,” such as test datasets, pseudo-random simulation data, or cache key generation. This article explains how to use mt_srand in combination with json_encode to generate predictable (i.e., reproducible) data samples, along with some practical tips.
mt_srand is the seed initialization function for mt_rand. Its role is to make the subsequent mt_rand calls produce predictable results based on the given seed. In other words, if you call mt_srand with the same seed, all subsequent mt_rand calls will return the same “random” number sequence.
<span><span><span class="hljs-title function_ invoke__">mt_srand</span></span><span>(</span><span><span class="hljs-number">1234</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">mt_rand</span></span><span>(); </span><span><span class="hljs-comment">// Always returns the same result</span></span><span>
</span></span>
json_encode converts arrays or objects into JSON strings. Its purpose is to standardize your generated pseudo-random data sequence into string format, making it easier to store, transmit, or compare.
By combining mt_srand and json_encode, you can generate a dataset and output it in structured JSON format, which remains entirely predictable when initialized with the same seed.
<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">generateSample</span></span><span>(</span><span><span class="hljs-params"><span class="hljs-variable">$seed</span></span></span><span>, </span><span><span class="hljs-variable">$count</span></span><span> = </span><span><span class="hljs-number">5</span></span><span>) {
</span><span><span class="hljs-title function_ invoke__">mt_srand</span></span><span>(</span><span><span class="hljs-variable">$seed</span></span><span>); </span><span><span class="hljs-comment">// Initialize with the specified seed</span></span><span>
</span><span><span class="hljs-variable">$data</span></span><span> = [];
</span><span><span class="hljs-variable">$data</span></span><span>[] = [
</span><span><span class="hljs-string">'id'</span></span><span> => </span><span><span class="hljs-variable">$i</span></span> + </span><span><span class="hljs-number">1</span></span><span>,
</span><span><span class="hljs-string">'value'</span></span><span> => </span><span><span class="hljs-title function_ invoke__">mt_rand</span></span><span>(</span><span><span class="hljs-number">1000</span></span><span>, </span><span><span class="hljs-number">9999</span></span><span>),
</span><span><span class="hljs-string">'timestamp'</span></span><span> => </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">'Y-m-d H:i:s'</span></span><span>, </span><span><span class="hljs-title function_ invoke__">mt_rand</span></span><span>(</span><span><span class="hljs-title function_ invoke__">strtotime</span></span><span>(</span><span><span class="hljs-string">'2020-01-01'</span></span><span>), </span><span><span class="hljs-title function_ invoke__">strtotime</span></span><span>(</span><span><span class="hljs-string">'2025-01-01'</span></span><span>)))
];
}
</span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-title function_ invoke__">json_encode</span></span><span>(</span><span><span class="hljs-variable">$data</span></span><span>, JSON_PRETTY_PRINT);
}
// Example: Using the same seed
echo generateSample(42);
Running generateSample(42) multiple times will consistently produce the same JSON data, ensuring high predictability.
In unit testing, consistent input data is crucial for troubleshooting issues. You can use seed-controlled data to fully reproduce tests when they fail.
<span><span><span class="hljs-variable">$data</span></span><span> = </span><span><span class="hljs-title function_ invoke__">json_decode</span></span><span>(</span><span><span class="hljs-title function_ invoke__">generateSample</span></span><span>(</span><span><span class="hljs-number">1001</span></span><span>), </span><span><span class="hljs-literal">true</span></span><span>);
</span><span><span class="hljs-comment">// Use $data for your testing logic</span></span><span>
</span></span>
When developing front-end features, this method can generate mock data before the API is ready, useful for page display and interaction testing.
Generate data with a seed based on specific rules and use json_encode to output it as part of cache keys, ensuring consistency and predictability.
Choose stable seeds: It is recommended to derive seeds from business-related IDs, timestamps, etc., for reuse later.
Use JSON_PRETTY_PRINT for readability: Pretty-printed JSON output is easier to inspect during debugging.
Be aware of global effects: mt_srand is global and affects all subsequent mt_rand calls. Avoid unintended side effects by saving state within functions or using modern alternatives like random_int.
By combining mt_srand and json_encode, we can easily create well-structured JSON data samples with controllable results. This approach is especially useful for testing, demos, and debugging, greatly simplifying development. We hope the examples and tips in this article are helpful.