<?php | |
declare(strict_types=1); | |
$fp=fopen("4_billion_numbers.txt","xb"); | |
if(!$fp){die("ERROR CREATING FILE");} | |
$str=""; | |
$chunk_size = 1*1024*1024*1024; | |
for($i=0;$i<4_000_000_000;++$i){ | |
// random_int() is a 64bit cryptograpically secure RNG, using https://man7.org/linux/man-pages/man2/getrandom.2.html | |
// (aka /dev/urandom ) | |
$str.=(string)random_int(0,PHP_INT_MAX); | |
$str.="\n"; | |
if(strlen($str)>$chunk_size){ | |
echo "."; | |
$written = fwrite($fp,$str); | |
if($written!==strlen($str)){ | |
echo "\n"; | |
throw new \RuntimeException("tried to write ".strlen($str)." bytes but could only write {$written} bytes"); | |
} | |
$str = ""; | |
} | |
} | |
if(strlen($str)>0){ | |
$written = fwrite($fp,$str); | |
if($written!==strlen($str)){ | |
echo "\n"; | |
throw new \RuntimeException("AT THE VERY LAST WRITE, tried to write ".strlen($str)." bytes but could only write {$written} bytes"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment