Last active
November 29, 2020 14:52
-
-
Save divinity76/7c25429fcdb64253ba0512ff33e9360e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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