Generating Random Files in Windows - Stack Overflow
Generating Random Files in Windows - Stack Overflow
Questions Asked 17 years, 6 months ago Modified 2 years, 9 months ago Viewed 104k times
AI Assist
Does anyone have a way to generate files of random data in Windows? I would like to generate
Tags Try Stack Overflow for Agents
50,000 small (2K) files as an example.
Verified knowledge for the agentic era
Stack Overflow for 46
windows Paste this into your AI coding assistant:
Agents
Articles
Supports Claude Code, Cursor, Codex,
Users Windsurf and more.
2 Could you share with us whether you want text files or binary files? Could you also comment as to what Explore Stack Overflow for Agents
programming languages you have access to (if any)? Do you have MS Office installed? Also, not very
Companies relevant, but what version of windows are you using? – user62572 Feb 10, 2009 at 20:05
1 Reply
Linked
Add a comment
1 Improve Powershell Performance to
Generate a Random File
This is lightning fast, compared to @user188737 solution. 0 Program for generating files?
Improve Powershell Performance to Generate a Random File Using PWM from a microcontroller to generate an
adjustable VREF for an LM393 comparator
Share Improve this answer Follow edited May 23, 2017 at 12:32 answered Sep 27, 2013 at 18:23 Examples of complete metric spaces with non-
complete set of distances
Community Bot Gerrit
1 1 891 6 13 Will creases to back cover of a passport be a
problem at immigration/check-in
Ray Woodcock Over a year ago Odd behaviour after saving a note for a contact
There are quite a few programs and cloud tools of this nature: Does the Buddhist precept against false speech
[Link]/2021/08/21/dummy-files require silence when telling the truth would cause
harm?
0 Reply
GBC compatible balls
Question feed
Since you don't specify a language, I'll simply pick one at random. Here is a powershell script to do
it:
9
$rootDir = 'C:\Temp\TestRandomFiles\'
$baseFile = $rootDir + "[Link]"
$desiredFileSize = 2*1KB
$fileCount = 50000
"start" | Out-File -Filepath $baseFile
While ($(Get-ChildItem -path $baseFile).Length -lt $desiredFileSize)
{
$(Get-ChildItem -path $baseFile).Length | Out-File $baseFile -APPEND
}
for($i=1;$i -lt $fileCount;$i++)
{
Copy-Item $baseFile "File$[Link]"
}
You'll have to change the variables to the parameters that you want of course.
Share Improve this answer Follow answered Feb 10, 2009 at 19:47
EBGreen
38.1k 12 70 86
4 Comments
Add a comment
1 Reply
1 Reply
0 Reply
0 Reply
Add a comment
Instead of using Get-Random to generate the text as per user188737 & mguassa suggestions, I
improved the speed by using GUIDs.
5
Function New-RandomFile {
Param(
$Path = '.',
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
(1..($FileSize/128)).foreach({-join ([guid]::NewGuid().Guid + [guid]::NewGuid().G
}
UPDATE:
I've updated my function to use a ScriptBlock, so you can replace the 'NewGuid()' method with
anything you want.
In this scenario, I make 1kb chunks, since I know I'm never creating smaller files. This improved the
speed of my function drastically!
Set-Content forces a NewLine at the end, which is why you need to remove 2 Characters each time
you write to file. I've replaced it with [[Link]]::WriteAllText() instead.
Function New-RandomFile_1kChunks {
Param(
$Path = (Resolve-Path '.').Path,
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
$Chunks = [math]::Ceiling($FileSize/1kb)
If you dont care that all chunks are random, you can simply Invoke() the generation of the 1kb
chunk once.. this improves the speed drastically, but won't make the entire file random.
Function New-RandomFile_Fast {
Param(
$Path = (Resolve-Path '.').Path,
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
Share Improve this answer Follow edited Apr 3, 2017 at 23:04 answered Apr 1, 2017 at 2:39
Marc Kellerman
466 4 10
Comments
Add a comment
None of the answers here were cutting it for me, so here's a script which takes advantage of the
Cryptography library to generate lots of random files.
3
This will generate files very quickly until your system's entropy is exhausted (on my PC this was
about 4,000 files). After this it (and any other applications on your system that need cryptographic
random numbers) will run very slowly. (In Linux terms, consider this script to use /dev/random
instead of /dev/urandom )
Copy
$directory = (Get-Location).Path;
0..10000 | ForEach-Object {
$size = 1023 * (Get-Random -Minimum 10 -Maximum 1536);
$contents = [Byte[]]::new($size);
$rng = [[Link]]::new();
$[Link]($contents);
If you'd rather not deplete your system's entropy, replace the two $rng lines with these:
Copy
$rng = [[Link]]::new();
$[Link]($contents);
This will run much slower, but for large quantities of files (or larger files) it should be more reliable.
Share Improve this answer Follow answered Apr 15, 2020 at 0:57
Yoshi Walsh
2,137 5 27 53
Comments
Add a comment
You'll have to create files in the normal way, and then populate them with randomized data,
probably from a rand() function of some sort.
2
It really depends on your programming language. Windows itself certainly won't provide this
capability.
There are a number of programming languages that could do this easily, however, including basic
windows batch/CMD scripts. What language are you interested in using?
Share Improve this answer Follow answered Feb 10, 2009 at 18:54
levand
8,530 4 44 56
Comments
Add a comment
language is powershell. assumptions: filenames will be sequential and not random. file contents
2
are to be cryptographically secure and unique. file location to be C:\temp\
#create a fixed size byte array for later use. make it the required file size.
$bytearray = New-Object byte[] 2048
#create and start a stopwatch object to measure how long it all takes.
$stopwatch = [[Link]]::StartNew()
$[Link]/50000
9.47536
so, thats about 10ms per file. Thats to an old sata disk.
Share Improve this answer Follow answered Jun 18, 2018 at 13:59
psomatic
21 2
1 Comment
Add a comment
0 Reply
Yes, fsutil is great, but doesn't generate random data, just ASCII nulls.
I don't remember where I found this but searching on google these days I can still find it at:
1
[Link]
I don't know how old this program is but at least as old as your question, probably somewhat
older.
Anyway here's a program in C which creates files with a chi-squared test result of 0:
// ------------------------------------------------------------
// Name: random.c (program to create random files)
//
// This "no-frills" program creates files with the following
// characteristics:
//
// (1) Byte sequences are random (no predictability);
// (2) Files produced are binary files;
// (3) File sizes are multiples of 256;
// (4) Files will have a chi-squared test result of 0
// (see [Link] by Wenger for explanation)
//
// Programmer: Scott Wenger
// Box 802
// Stevens Point, WI 54481
// panther@[Link]
//
// Note: part of this code is from Knuth Volume II
//
// Enhancements and modifications of this program are left
// to the imagination and creativity of the programmer.
// Check your compiler for required header files. You may
// need to include the iostream header.
//
// Random files are of potential use to cryptographers
// for the purpose of encryption.
//
// To analyze files produced by this program, see
// the [Link] program by Scott Wenger (found at
// [Link]
// ------------------------------------------------------------
Share Improve this answer Follow answered Aug 10, 2016 at 13:38
Rubenisme
906 1 9 19
Comments
Add a comment
for those who found this looking for a way to overwrite unused space on drive with random data,
the following command in cmd will do:
1
cipher /w:X:\
it will first write 0x00's then 0xFF's then random data on the unallocated space (leaving the files
alone). it takes some time.
Share Improve this answer Follow edited Dec 6, 2022 at 13:45 answered Dec 6, 2022 at 13:38
robotik
2,037 1 22 26
Comments
Add a comment
Share Improve this answer Follow answered Feb 10, 2009 at 18:54
jgallant
11.3k 2 42 72
Comments
Add a comment
Or File generator
0
Share Improve this answer Follow answered Feb 10, 2009 at 19:00
beach
8,720 4 34 26
1 Comment
Add a comment
1 Reply
You could use VBA in excel if you have limited permissions on the machine that you are on. This
would create txt files to the number required with random numbers. Probably not the quickest of
0 ways of going about this though.
Sub rndcreate()
[Link] = False
[Link] = False
folder = "C:\test\"
totalfiles = 1
totalentries = 150
upperbound = 99999
lowerbound = 1
For p = 1 To totalfiles
For i = 1 To totalentries
Share Improve this answer Follow answered Apr 11, 2017 at 11:56
Crellin
1 2
Comments
Add a comment
edit
I re-read the question, the following will not provide the answer (50x2k files) as is, but will create
0
arbitrarily sized files with truly random binary data.
Please comment if you would like to see an example that answers the question exactly.
/edit
The following can generate a 1GB file of cryptographically secure random data using objects
available in powershell:
#captain obvious
$[Link]()
$[Link]()
$[Link]($bytearray)
with
$[Link]($bytearray)
!msdn [Link]
you will be given extremely focussed results direct from the Microsoft Developer Network,
allowing you to see the Crypto classes, methods and properties available.
Share Improve this answer Follow edited Jun 18, 2018 at 13:13 answered Jun 18, 2018 at 12:37
psomatic
21 2
Comments
Add a comment
Well, a bit late, but this is my contribution to a problem that will always be actual.
$currdrive="G"
for ($j=0;$j -lt 5;$j++){
new-item "$($currdrive):\random$($j)" -itemType directory
for ($i=0;$i -lt 100;$i++){
echo "Creating file $i...";
$size=1048576*1024;
$out = new-object byte[] $size;(new-object Random).NextBytes($out);[[Link]]
}
}
Share Improve this answer Follow answered Dec 29, 2020 at 11:47
Daniel J.
397 2 12
Comments
Add a comment
0 $rng = [[Link]]::new();
$total = 100
0..$total | % {
$p = $(Join-Path $pwd ("$_".ToCharArray() -join '/')) + ".txt"
$d = [[Link]]::GetDirectoryName($p)
New-Item -ItemType Directory -Path $d -ErrorAction SilentlyContinue | Out-Null
$size = 1023 * (Get-Random -Minimum 1 -Maximum 512);
$contents = [Byte[]]::new($size);
$[Link]($contents);
Set-Content -Force -Path $p -Value $contents
if (($_%10) -eq 0) { Write-Host -NoNewline "$(($_/$total)*100)% " }
}; echo ""
Share Improve this answer Follow answered Oct 24, 2023 at 19:29
Jonathan DeMarks
2,449 2 16 14
Comments
Add a comment
Your Answer
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our
Post Your Answer
privacy policy.