Create a random number with low precision MATLAB

I want to create a large number of random numbers (evenly distributed over the interval [0,1]). Currently, generating these random numbers causes my program to work quite slowly, but the program only needs to calculate them up to about 5 decimal places.

I'm not quite sure how MATLAB generates random numbers, but if there is a way to only calculate them up to 5 decimal places, then it (I hope, strongly) will speed up my program.

Is there any way to do this?

Thank you very much.

+4
source share
3 answers

To answer your question, yes, you can generate random numbers with single precision, for example:

r = rand(..., 'single'); %Reference: http://www.mathworks.com/help/matlab/ref/rand.html 

Numbers with one precision have 7 (ish) significant digits when printed as decimal.

To repeat some of the comments above, I don’t think it will buy you more performance. The first thing to do if rand is really your slow operation is batch call processing. That is, instead of:

 for ix 1:1000 y = rand(1,1,'single); end 

using:

 yVector = rand(1000,1,'single'); 
+2
source

As already mentioned, you can give the RAND command to generate numbers directly as a single precision, and it is definitely best to generate numbers in a decent size. If you still need more performance and you have the Parallel Computing Toolbox and the supported NVIDIA GPU, the gpuArray.rand function can be even faster, especially if you select the philox generator as follows:

 parallel.gpu.RandStream('Philox4x32-10') 
+2
source

Assuming you actually have the right code layout in which you generate a lot of numbers in an array, this might be a solution for low precision. Please note that I have not tested, but it is mentioned as quickly:

 R = randi([0 100000],500,300)/100000 

This will produce 150,000 random numbers with low precision between 0 and 1

0
source

All Articles