Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active November 29, 2020 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/7c25429fcdb64253ba0512ff33e9360e to your computer and use it in GitHub Desktop.
Save divinity76/7c25429fcdb64253ba0512ff33e9360e to your computer and use it in GitHub Desktop.
<?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