Function Repository Resource:

CreateRandomFile

Source Notebook

Create a file filled with random bytes for testing

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["CreateRandomFile"]["n"]

creates a random file of n bytes in the default area for temporary files on your computer system.

ResourceFunction["CreateRandomFile"][path,n]

creates a random file at the location specified by path.

Details and Options

The value for path can be any of the following:
"file"a string corresponding to a local file path
File["file"]a local file path
LocalObject[]a LocalObject
CloudObject[]a CloudObject
If "file" has no pathname separators, ResourceFunction["CreateRandomFile"] creates a file in your current working directory.
A relative path specified by "file" is interpreted with respect to the current working directory.
ResourceFunction["CreateRandomFile"] only creates the file; it does not open it.
The file created can subsequently be opened for reading or writing in binary mode.
ResourceFunction["CreateRandomFile"] returns the full name of the file it creates and a Failure object if it cannot create the file.
ResourceFunction["CreateRandomFile"][n] creates a file in the directory specified by the current value of $TemporaryDirectory.
ResourceFunction["CreateRandomFile"][n] is equivalent to ResourceFunction["CreateRandomFile"][Automatic,n].
The default setting CreateIntermediateDirectoriesTrue specifies that intermediate directories should be created as needed.
File["path"] may be used to specify a file name.

Examples

Basic Examples (2) 

Create a file containing 50 random bytes:

In[1]:=
ResourceFunction["CreateRandomFile"][50]
Out[1]=

Create a random file at a specified path:

In[2]:=
ResourceFunction["CreateRandomFile"]["file.bin", 50]
Out[2]=

Verify the file size:

In[3]:=
FileSize["file.bin"]
Out[3]=

View the contents:

In[4]:=
ReadString["file.bin"]
Out[4]=

Clean up:

In[5]:=
DeleteFile["file.bin"]

Scope (5) 

Specify a path using a File object:

In[6]:=
ResourceFunction["CreateRandomFile"][File["file.bin"], 50]
Out[6]=
In[7]:=
DeleteFile[%]

Create a random LocalObject:

In[8]:=
ResourceFunction["CreateRandomFile"][LocalObject[], 100]
Out[8]=
In[9]:=
DeleteObject[%]

Create a random CloudObject:

In[10]:=
ResourceFunction["CreateRandomFile"][CloudObject[], 100]
Out[10]=
In[11]:=
DeleteObject[%]

Create a random CloudObject at a specific location:

In[12]:=
ResourceFunction["CreateRandomFile"][CloudObject["path/to/file"], 100]
Out[12]=
In[13]:=
DeleteObject[%]

Specify Permissions:

In[14]:=
ResourceFunction["CreateRandomFile"][
 CloudObject["path/to/file", Permissions -> "Public"], 100]
Out[14]=
In[15]:=
DeleteObject[%]

Options (4) 

CreateIntermediateDirectories (2) 

By default, the parent directory will be created for the file if it does not already exist:

In[16]:=
path = FileNameJoin[{$TemporaryDirectory, CreateUUID[], "path", "to", "file"}]
Out[16]=
In[17]:=
ResourceFunction["CreateRandomFile"][path, 50]
Out[17]=

Do not create a file if the parent directory does not exist:

In[18]:=
path = FileNameJoin[{$TemporaryDirectory, CreateUUID[], "path", "to", "file"}]
Out[18]=
In[19]:=
ResourceFunction["CreateRandomFile"][path, 50, CreateIntermediateDirectories -> False]
Out[19]=

OverwriteTarget (2) 

By default, CreateRandomFile will not overwrite existing files:

In[20]:=
file = CreateFile[]
Out[20]=
In[21]:=
ResourceFunction["CreateRandomFile"][file, 50]
Out[21]=

Overwrite the target file if it already exists:

In[22]:=
ResourceFunction["CreateRandomFile"][file, 50, OverwriteTarget -> True]
Out[22]=
In[23]:=
FilePrint[file]
In[24]:=
DeleteFile[file]

Applications (2) 

Create a file for testing performance of different file hashing methods:

In[25]:=
file = ResourceFunction["CreateRandomFile"][1000000]
Out[25]=
In[26]:=
timings = AssociationMap[
  First[RepeatedTiming[FileHash[file, #]]] &,
  {"Adler32", "CRC32", "Keccak224", "Keccak256", "Keccak384", "Keccak512", "MD2", "MD4", "MD5", "RIPEMD160", "RIPEMD160SHA256", "SHA", "SHA256", "SHA256SHA256", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512", "SHA384", "SHA512"}]
Out[26]=
In[27]:=
BarChart[ReverseSort[timings], ChartLabels -> Automatic, ScalingFunctions -> "Log", BarOrigin -> Left]
Out[27]=

Create a cloud object with random data for testing download speeds:

In[28]:=
obj = ResourceFunction["CreateRandomFile"][
  CloudObject[Permissions -> "Public"], 25000000]
Out[28]=
In[29]:=
Dataset[URLResponseTime[First[obj], All]]
Out[29]=
In[30]:=
DeleteObject[obj]

Properties and Relations (2) 

CreateRandomFile writes data in batches so memory usage will not exceed what is available:

In[31]:=
MemoryAvailable[]
Out[31]=
In[32]:=
MaxMemoryUsed[
 file = ResourceFunction["CreateRandomFile"][50000000000]]
Out[32]=
In[33]:=
DeleteFile[file]

Specifying zero bytes is effectively equivalent to using CreateFile:

In[34]:=
ResourceFunction["CreateRandomFile"][0]
Out[34]=
In[35]:=
File[CreateFile[]]
Out[35]=

Version History

  • 1.0.0 – 20 December 2021

Related Resources

License Information