嗨,我想生成一个随机数据矩阵,比如 row*col = 30000*500000。我尝试在 Excel 中使用 VBA,但速度很慢;即使我在我的 8G 内存盒上使用了 bigmemory 包,64 位 R 也被冻结了。为了尽快完成它,我应该使用 C 吗?Java 8 中的并行编程对这个问题有帮助吗?有人有过这方面的经验吗?非常感激!
问问题
81 次
1 回答
0
如果每个随机数占用四个字节,则总共需要 60000000000 个字节,即 60e9 字节或 55 GiB。难怪您不能在 8 GiB 计算机上一次将它们全部保存在内存中。
如果您真的需要那么多随机数(出于什么目的?),您唯一的机会是将它们写入一个大文件,然后通过文件访问使用它们。或者,只需在需要时即时生成它们;什么更好取决于您的特定应用程序。
如果您还需要高质量,我建议使用具有良好内置随机数生成器的编译器/库。
这是一个简短的 Fortran 示例程序,说明了编写此类文件的程序的外观。随意适应您选择的语言。
program random
implicit none
integer, parameter :: nx = 30000, ny=500000
real, dimension(ny) :: r ! A real array of length ny, i.e. 500000
integer :: i
open(20,file="random.dat",form="unformatted",access="stream") ! Byte stream access
do i=1,nx ! Do this nx times
call random_number(r) ! Fill up the array with pseudorandom numbers
write (20) r ! Write it to the file
end do
close(20)
end program random
于 2015-03-05T07:50:01.103 回答