在 Windows 中生成随机文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/533636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating Random Files in Windows
提问by
Does anyone have a way to generate files of random data in Windows? I would like to generate 50,000 small (2K) files as an example.
有没有人有办法在 Windows 中生成随机数据文件?我想以生成 50,000 个小 (2K) 文件为例。
回答by Bogdan
You can run fsutil in a batch loop to create files of any size.
您可以在批处理循环中运行 fsutil 来创建任何大小的文件。
fsutil file createnew filename.extension 2000
回答by David Waters
I have been using Random Data File Creatorand liking it, it creates binary files (i.e. not text files) filled with pseudo-random bits, it can quickly create very large files. To use it to create multiple small files you would need to script it, which would be very easy given it is command line.
我一直在使用Random Data File Creator并喜欢它,它创建了充满伪随机位的二进制文件(即不是文本文件),它可以快速创建非常大的文件。要使用它来创建多个小文件,您需要编写脚本,鉴于它是命令行,这将非常容易。
回答by Gerrit
You can use PowerShell to generate cheap random data for your files:
您可以使用 PowerShell 为您的文件生成廉价的随机数据:
[Byte[]] $out = @()
0..2047 | % {$out += Get-Random -Minimum 0 -Maximum 255}
[System.IO.File]::WriteAllBytes("myrandomfiletest", $out)
This uses an algorithm with a seed taken from the system clock, so don't use this for ANY serious cryptographic applications.
这使用了一种算法,其种子取自系统时钟,因此不要将其用于任何严重的加密应用程序。
In addition, be wary of the performance degradation of Get-Random
when increasing the size of the output file. More on this aspect here:
此外,请注意Get-Random
增加输出文件大小时的性能下降。更多关于这方面的信息:
回答by gwiazdorrr
One-liner in Powershell:
Powershell 中的单行:
$out = new-object byte[] 1048576; (new-object Random).NextBytes($out); [IO.File]::WriteAllBytes('d:\file.bin', $out)
This is lightning fast, compared to @user188737 solution.
与@user188737 解决方案相比,这是闪电般的速度。
回答by EBGreen
Since you don't specify a language, I'll simply pick one at random. Here is a powershell script to do it:
由于您没有指定语言,我将随机选择一种。这是一个powershell脚本来做到这一点:
$rootDir = 'C:\Temp\TestRandomFiles\'
$baseFile = $rootDir + "base.txt"
$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$i.txt"
}
You'll have to change the variables to the parameters that you want of course.
当然,您必须将变量更改为您想要的参数。
回答by levand
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.
您必须以正常方式创建文件,然后用随机数据填充它们,可能来自某种 rand() 函数。
It really depends on your programming language. Windows itself certainly won't provide this capability.
这真的取决于你的编程语言。Windows 本身当然不会提供此功能。
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?
然而,有许多编程语言可以轻松做到这一点,包括基本的 Windows 批处理/CMD 脚本。你有兴趣使用什么语言?
回答by Marc Kellerman
Instead of using Get-Random to generate the text as per user188737 & mguassa suggestions, I improved the speed by using GUIDs.
我没有根据 user188737 和 mguassa 建议使用 Get-Random 生成文本,而是通过使用 GUID 提高了速度。
Function New-RandomFile {
Param(
$Path = '.',
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
(1..($FileSize/128)).foreach({-join ([guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid -Replace "-").SubString(1, 126) }) | set-content "$Path$FileName"
}
This took 491 milliseconds to generate a 1mb file. Running:
生成一个 1mb 的文件需要 491 毫秒。跑步:
New-RandomFile -FileSize 1mb
UPDATE:
更新:
I've updated my function to use a ScriptBlock, so you can replace the 'NewGuid()' method with anything you want.
我已经更新了我的函数以使用 ScriptBlock,所以你可以用你想要的任何东西替换 'NewGuid()' 方法。
In this scenario, I make 1kb chunks, since I know I'm never creating smaller files. This improved the speed of my function drastically!
在这种情况下,我制作了 1kb 的块,因为我知道我从不创建较小的文件。这大大提高了我的功能的速度!
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 [io.file]::WriteAllText() instead.
Set-Content 在末尾强制换行,这就是为什么每次写入文件时都需要删除 2 个字符。我用 [io.file]::WriteAllText() 代替了它。
Function New-RandomFile_1kChunks {
Param(
$Path = (Resolve-Path '.').Path,
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
$Chunk = { [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid -Replace "-" }
$Chunks = [math]::Ceiling($FileSize/1kb)
[io.file]::WriteAllText("$Path$FileName","$(-Join (1..($Chunks)).foreach({ $Chunk.Invoke() }))")
Write-Warning "New-RandomFile: $Path$FileName"
}
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.
如果你不关心所有的块都是随机的,你可以简单地 Invoke() 1kb 块的生成一次..这大大提高了速度,但不会使整个文件随机。
Function New-RandomFile_Fast {
Param(
$Path = (Resolve-Path '.').Path,
$FileSize = 1kb,
$FileName = [guid]::NewGuid().Guid + '.txt'
)
$Chunk = { [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid +
[guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid + [guid]::NewGuid().Guid -Replace "-" }
$Chunks = [math]::Ceiling($FileSize/1kb)
$ChunkString = $Chunk.Invoke()
[io.file]::WriteAllText("$Path$FileName","$(-Join (1..($Chunks)).foreach({ $ChunkString }))")
Write-Warning "New-RandomFile: $Path$FileName"
}
Measure-Command all these changes to generate a 10mb file:
Measure-Command 将所有这些更改生成一个 10mb 的文件:
Executing New-RandomFile: 35.7688241 seconds.
执行 New-RandomFile:35.7688241 秒。
Executing New-RandomFile_1kChunks: 25.1463777 seconds.
执行 New-RandomFile_1kChunks:25.1463777 秒。
Executing New-RandomFile_Fast: 1.1626236 seconds.
执行 New-RandomFile_Fast:1.1626236 秒。
回答by beach
How about something like this: Random File Generator 1.1
这样的事情怎么样: 随机文件生成器 1.1
回答by jgallant
Well, technically you could write something to do this for you.
I don't know of anything specific.. but the easiest way would be to create a TEXT file of a specific size (2K for example).. then write a batch file to copy it 50000 times.
好吧,从技术上讲,你可以写一些东西来为你做这件事。
我不知道有什么具体的……但最简单的方法是创建一个特定大小的文本文件(例如 2K)……然后编写一个批处理文件将其复制 50000 次。
回答by Rubenisme
Yes, fsutil is great, but doesn't generate random data, just ASCII nulls.
是的,fsutil 很棒,但不会生成随机数据,只会生成 ASCII 空值。
I don't remember where I found this but searching on google these days I can still find it at: http://www.private-files.com/other/random.c.txt
我不记得我在哪里找到的,但这些天在 google 上搜索我仍然可以在以下位置找到它:http: //www.private-files.com/other/random.c.txt
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:
无论如何,这里有一个 C 语言程序,它创建卡方测试结果为 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 analyze.exe by Wenger for explanation)
//
// Programmer: Scott Wenger
// Box 802
// Stevens Point, WI 54481
// [email protected]
//
// 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 analyze.exe program by Scott Wenger (found at
// http://www.coredcs.com/sware.html)
// ------------------------------------------------------------
// This program works in the following way:
// The time is used to seed the random number generator.
// Using Knuth's algorithm, random numbers are generated
// in the range of 0 to 255 (corresponding to 256 ASCII chars.)
// When random numbers are generated they are marked as used and
// are not re-used until all 256 ASCII values appear. Characters
// are written to disk and the process continues until the
// desired file size is reached. Output is a random binary file
// called random.bin (placed in the root directory)
// The controlled filesize along with the placeholder feature
// of this code forces a very high degree of randomness in
// the output file.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void init_mm();
void clear_array();
int number_range(int minval, int maxval);
int number_mm();
static int rgiState[2 + 55];
int place_holder[256]; // to keep track of numbers already generated
int main()
{
mainprogram();
return 0;
}
int mainprogram()
{
int ch;
int c_used = 0; // counter of chars in placeholder
int done = 0;
int random;
char buffer[2];
long x;
long byte_size = 0L;
FILE *fp;
clear_array();
init_mm(); // seed random number generator
// create a random file of length specified by user
printf("\nrandom.exe originally by Scott Wenger");
printf("\nThis program creates a random binary file.\n");
printf("\nPlease specify length of random file to create (in megabytes): ");
scanf("%ld", &byte_size);
while (byte_size > 1000 || byte_size <= 0 )
{
printf("\nWill not create files larger than a gigabyte! ");
printf("\nPlease specify length of random file to create (in megabytes): ");
flushall();
scanf("%ld", &byte_size);
}
byte_size = byte_size * 1024 * 1024;
if ( (fp = fopen("random.bin", "wb")) == NULL) {
fprintf(stderr, "\nOutput file (random.bin) could not be created.");
fflush(stdout);
exit(1);
}
for (x = 0L; x < byte_size; x++) {
if (c_used == 256) {
clear_array();
c_used = 0;
}
random = number_range(0, 255); // use all ASCII values
if ( *(place_holder + random) ) { // already used, find another
done = 0;
while (!done) {
random = number_range(0, 255);
if ( *(place_holder + random) == 0) {
*(place_holder + random) = 1;
done = 1;
}
}
}
else *(place_holder + random) = 1; // use it and mark as used
c_used++; // found next character so increment counter
sprintf(buffer, "%c", random); // convert ASCII value to char
ch = buffer[0];
fputc(ch, fp); // write to file
}
fclose(fp);
printf("\nDone. File \"random.bin\" was created (size: %ld bytes)", byte_size);
printf("\nOutput file is in the root directory (c:\random.bin)\n");
return(0);
}
// ---------------------------------------------------------------------------------
void clear_array()
{
register int x;
for (x = 0; x < 256; x++)
*(place_holder + x) = 0;
}
// ---------------------------------------------------------------------------------
int number_mm()
{
int *piState;
int iState1;
int iState2;
int iRand;
piState = &rgiState[2];
iState1 = piState[-2];
iState2 = piState[-1];
iRand = ( piState[iState1] + piState[iState2] )
& ( ( 1 << 30 ) - 1 );
piState[iState1] = iRand;
if ( ++iState1 == 55 ) iState1 = 0;
if ( ++iState2 == 55 ) iState2 = 0;
piState[-2] = iState1;
piState[-1] = iState2;
return(iRand >> 6);
}
// ---------------------------------------------------------------------------------
// Generate a random number.
int number_range( int minval, int maxval )
{
int power, number;
if ( ( maxval = maxval - minval + 1 ) <= 1 ) return (minval);
for ( power = 2; power < maxval; power <<= 1 )
;
while ( ( number = number_mm( ) & ( power - 1 ) ) >= maxval )
;
return(minval + number);
}
// ---------------------------------------------------------------------------------
// Mitchell-Moore algorithm from Knuth Volume II.
void init_mm( )
{
int *piState;
int iState;
piState = &rgiState[2];
piState[-2] = 55 - 55;
piState[-1] = 55 - 24;
piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 );
piState[1] = 1;
for ( iState = 2; iState < 55; iState++ )
{
piState[iState] = ( piState[iState-1] + piState[iState-2] )
& ( ( 1 << 30 ) - 1 );
}
}
// -------------------- End -------------------------------------------------------