您好我想生成一个随机数据矩阵,比如row * col = 30000 * 500000。我尝试在Excel中使用VBA,减慢速度;即使我在8G内存盒上使用了bigmemory包,64位R也被冻结了。为了尽可能快地完成它,我应该使用C吗? Java 8中的并行编程是否对此问题有帮助?有人有这方面的经验吗?非常感谢!
答案 0 :(得分:0)
如果每个随机数占用4个字节,则需要总共60000000000个字节,即60e9字节或55 GiB。毫无疑问,您无法在8 GiB计算机上将它们全部保存在内存中。
如果你真的需要那么多随机数(出于什么目的?),你唯一的机会就是将它们写入一个大文件,然后通过文件访问来使用它们。或者,只需在需要时即时生成它们;什么是更好取决于您的特定应用。
如果您还需要高质量,我建议使用带有良好内置随机数gernerator的编译器/库。
这是一个简短的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