在Windows中生成随机文件

有没有人有办法在Windows中生成随机数据文件? 我想以5万个小文件(2K)为例。

您可以在批处理循环中运行fsutil以创建任何大小的文件。

fsutil file createnew filename.extension 2000 

我一直在使用随机数据文件创建器和喜欢它,它创建二进制文件(即不是文本文件)填充伪随机位,它可以快速创建非常大的文件。 要使用它来创建多个小文件,您需要对其进行编写,因为它是命令行。

您可以使用PowerShell为您的文件生成便宜的随机数据:

 [Byte[]] $out = @() 0..2047 | % {$out += Get-Random -Minimum 0 -Maximum 255} [System.IO.File]::WriteAllBytes("myrandomfiletest", $out) 

这使用从系统时钟中取出种子的算法,所以不要将其用于任何严重的加密应用程序。

另外,当增加输出文件的大小时,要小心Get-Random的性能下降。 更多关于这方面的内容:

  • PowerShell随机文件生成器太慢
  • 提高Powershell性能以生成随机文件

既然你没有指定一种语言,我会随便选一个。 这是一个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" } 

你将不得不将变量改变为你想要的参数。

您将不得不以正常的方式创建文件,然后用随机数据填充它们,可能来自某种rand()函数。

这真的取决于你的编程语言。 Windows本身肯定不会提供这种功能。

有许多编程语言可以很容易地完成这个任务,但是,包括基本的Windows批处理/ CMD脚本。 你有兴趣使用哪种语言?

Powershell中的一行代码:

 $out = new-object byte[] 1048576; (new-object Random).NextBytes($out); [IO.File]::WriteAllBytes('d:\file.bin', $out) 

与@ user188737解决方案相比,这是闪电般的。

如何这样的事情: 随机文件生成器1.1

或文件生成器

而不是使用获取随机生成文本按照user188737&mguassa建议,我使用GUIDs提高了速度。

 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" } 

这花了491毫秒来生成一个1MB的文件。 运行:

 New-RandomFile -FileSize 1mb 

更新:

我已经更新了我的函数来使用ScriptBlock,所以你可以用你想要的任何东西来替换“NewGuid()”方法。

在这种情况下,我做了1kb块,因为我知道我从来没有创建更小的文件。 这大大提高了我的功能的速度!

Set-Content在最后强制NewLine,这就是为什么每次写入文件时都需要删除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" } 

如果你不关心所有的块是随机的,你可以简单地调用()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" } 

测量 – 命令所有这些更改生成一个10MB的文件:

执行New-RandomFile:35.7688241秒。

执行New-RandomFile_1kChunk:25.1463777秒。

执行New-RandomFile_Fast:1.1626236秒。

那么,从技术上讲,你可以写些东西来为你做这个。
我不知道任何具体的东西..但最简单的方法是创建一个特定大小的文本文件(例如2K)..然后写一个批处理文件复制50000次。

是的,fsutil是伟大的,但不会产生随机数据,只是ASCII空值。

我不记得我在哪里找到这个,但在谷歌搜索这些天,我仍然可以找到它在: http : //www.private-files.com/other/random.c.txt

我不知道这个程序有多老,但至少和你的问题一样老,可能有点老。

无论如何,这里是一个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 // panther@wctc.net // // 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 ------------------------------------------------------- 

如果您在机器上的权限有限,则可以在Excel中使用VBA。 这将创建txt文件到随机数所需的数字。 可能不是最快的方法去做这个。

 Sub rndcreate() Application.ScreenUpdating = False Application.DisplayAlerts = False Dim sbook As Workbook Dim i As Double Dim upperbound, lowerbound, totalentries, totalfiles As Integer Dim x, folder, file As String 'Set output location folder = "C:\test\" 'Number of files created and entries in files as below totalfiles = 1 totalentries = 150 upperbound = 99999 lowerbound = 1 For p = 1 To totalfiles 'Add new workbook to populate with data Set sbook = Workbooks.Add 'Set file name file = "randomdatafile" & p For i = 1 To totalentries 'Randomly created integers between your two bounds x = ((upperbound - lowerbound + 1) * Rnd + lowerbound) Range("A" & i) = x Next ActiveWorkbook.SaveAs Filename:=folder & file & ".txt", FileFormat:=xlTextWindows ActiveWorkbook.Close Next End Sub