For testing purposes I need a function like this:
/**
* @param int $sizeInBytes
*
* @returns string with random data
*/
function randomData($sizeInBytes)
{
...
}
Any ideas of a effective implementation? There is need for speed but not for real randomness (more a kind of “lorem ipsum”). My simplest idea would be to use real large file in the filesystem and fetch the required size by stream. But this needs at least a 100MB file. Is there a better way?
How about just creating a very long string if you have the memory available anyways?
That should not take all that long 🙂
You could of course just write that to a file one but creating it in memory doesn’t really take all that long.
The code above produces an
98MBstring in about100msand creating a200MBstring takes about170mson my box. That should be good enough for most cases.As noted in the comment below: You might have to change your php.ini setting if you limit the amount of memory your script is allowed to consume (or change it via
memory_limit('...');). Also strings > 1.5gb might cause issues but thats not a concern here I’d say.