random number
mGill
Have trouble generating single number between 0 and 1 with variable precision. In some cases I need a number with 4 decimal places and another time with 20. Should be a function like: returnRandomNumber(start, end, decimal points)
-Max
I'm a bit mystified as to why you want the digits tho'. If you just want to print out the number to the history line you could use:
July 19, 2009 at 07:44 pm - Permalink
Function returnRandomNumber(decimalpoints)
variable decimalpoints
return round((enoise(0.5)+0.5)*10^decimalpoints)/10^decimalpoints
End
Function MCtest(decimalpoints,n)
variable decimalpoints,n
variable i
make /o/n=(n) w
for (i=0; i<n; i+=1)
w[i] = returnRandomNumber(decimalpoints)
endfor
Make/N=10/O w_Hist;DelayUpdate
Histogram/B={0,0.1,10} w,w_Hist;DelayUpdate
End
John Bechhoefer
Department of Physics
Simon Fraser University
Burnaby, BC, Canada
July 20, 2009 at 06:01 pm - Permalink
ANother way could be to produce a random number using enoise. Then do a sprintf operation to put it into a string, truncated to the right numbe of digits. Then use str2num to get the number back again. However, the sprintf rounds the last digit so the other way is probably better.
July 20, 2009 at 06:54 pm - Permalink
variable decimalpoints
return floor((enoise(0.5)+0.5)*10^decimalpoints)/10^decimalpoints
End
Function MCtest(decimalpoints,n)
variable decimalpoints,n
variable i
make /o/d/n=(n) w
for (i=0; i<n; i+=1)
w[i] = returnRandomNumber(decimalpoints)
endfor
Make/N=10/O w_Hist;DelayUpdate
Histogram/B={0,0.1,10} w,w_Hist;DelayUpdate
End
This can be tested with
Notice that all bins have 1e4*0.1 = 1000 ± sqrt(1000) counts and that repeated execution shows that fluctuations in all bins are symmetric about 1000 (the poor man's way of doing statistics....).
John Bechhoefer
Department of Physics
Simon Fraser University
Burnaby, BC, Canada
July 21, 2009 at 09:50 pm - Permalink