# How to create a random sample of values between 0 and 1 in R?

The continuous uniform distribution can take values between 0 and 1 in R if the range is not defined. To create a random sample of continuous uniform distribution we can use runif function, if we will not pass the minimum and maximum values the default will be 0 and 1 and we can also use different range of values.

## Examples

runif(5)
[1] 0.8667731 0.7109824 0.4466423 0.1644701 0.5611908
runif(10)
[1] 0.5923782 0.8793613 0.6912947 0.2963916 0.6076762 0.7683766 0.1143595
[8] 0.4782710 0.1143383 0.4540217
runif(50)
[1] 0.841674685 0.325249762 0.640847906 0.203868249 0.495230429 0.897175830
[7] 0.744447459 0.490173680 0.254711280 0.144844443 0.867749180 0.004405166
[13] 0.539785687 0.739637398 0.062214554 0.648021581 0.768686809 0.305543906
[19] 0.757496413 0.527085302 0.633331579 0.700118363 0.857950259 0.929350618
[25] 0.167015719 0.775870043 0.430343200 0.528408273 0.600575697 0.612206968
[31] 0.065904791 0.061135682 0.082027863 0.193586800 0.013956337 0.156875620
[37] 0.837501421 0.971202297 0.930835689 0.292126061 0.599263353 0.826630821
[43] 0.509235736 0.741715013 0.224485511 0.113099235 0.395143355 0.375654137
[49] 0.973050494 0.107550270
round(runif(50),2)
[1] 0.51 0.70 0.90 0.45 0.41 0.74 0.31 0.40 0.10 0.05 0.18 0.05 0.63 0.34 0.57
[16] 0.06 0.73 0.37 0.79 0.85 0.82 0.41 0.32 0.34 0.37 0.14 0.21 0.11 0.43 0.86
[31] 0.83 0.09 0.88 0.04 0.62 0.64 0.15 0.75 0.78 0.16 0.67 0.97 0.79 0.64 0.56
[46] 0.40 0.07 0.69 0.82 0.63
round(runif(50),4)
[1] 0.2951 0.2916 0.9049 0.2669 0.7613 0.2080 0.4739 0.1110 0.6155 0.5429
[11] 0.4490 0.2941 0.8262 0.7719 0.7896 0.7634 0.6260 0.7812 0.7600 0.6852
[21] 0.9142 0.0165 0.2324 0.0821 0.0814 0.4009 0.3315 0.8843 0.9684 0.1966
[31] 0.4841 0.5795 0.7898 0.1865 0.6929 0.8599 0.0492 0.8275 0.7431 0.3122
[41] 0.8480 0.3327 0.4872 0.0503 0.1887 0.0296 0.6011 0.1162 0.7776 0.6874
round(runif(50),5)
[1] 0.40368 0.33585 0.03557 0.06047 0.95041 0.18260 0.70011 0.75148 0.12414
[10] 0.01310 0.42343 0.05846 0.21341 0.05454 0.77823 0.66151 0.61406 0.59459
[19] 0.50299 0.96780 0.43033 0.64652 0.39697 0.05897 0.47169 0.79828 0.74154
[28] 0.56074 0.97303 0.35301 0.36110 0.67452 0.14553 0.45195 0.05780 0.90489
[37] 0.96745 0.28014 0.02089 0.77789 0.04797 0.03550 0.40495 0.08924 0.59908
[46] 0.89074 0.48498 0.47335 0.59422 0.00719
round(runif(100),2)
[1] 0.10 0.06 0.51 0.89 0.80 0.68 0.97 0.58 0.60 0.79 0.96 0.48 0.29 0.16 0.42
[16] 0.35 0.46 0.18 0.46 0.34 0.48 0.35 0.72 0.10 0.50 0.93 0.30 0.54 0.85 0.19
[31] 0.12 0.10 0.47 0.66 0.43 0.09 0.44 0.86 0.99 0.31 0.10 0.61 0.20 0.15 0.02
[46] 0.25 0.33 0.75 0.98 0.23 0.21 0.70 0.42 0.24 0.87 0.84 0.99 0.06 0.75 0.48
[61] 0.84 0.35 0.48 0.62 0.40 0.25 0.07 0.08 0.75 0.40 0.83 0.95 0.00 0.87 0.27
[76] 0.53 0.21 0.41 0.28 0.83 0.90 0.26 0.50 0.19 0.70 0.93 0.24 0.45 0.33 0.84
[91] 0.15 0.81 0.62 0.17 0.08 0.76 0.74 0.11 0.20 0.49
round(runif(150),1)
[1] 0.6 0.3 0.3 0.3 0.9 0.7 0.1 0.1 0.1 0.9 0.4 0.6 1.0 0.0 0.4 1.0 0.1 1.0
[19] 0.8 0.0 0.9 0.9 0.7 0.7 0.7 0.7 0.3 0.7 0.1 0.1 0.9 0.0 0.1 1.0 0.9 1.0
[37] 0.9 0.6 0.0 0.4 0.4 1.0 0.2 0.4 0.2 0.8 0.3 0.9 0.8 0.6 0.3 0.3 0.4 0.7
[55] 0.2 0.9 1.0 0.9 0.8 0.7 0.9 1.0 0.5 0.8 0.6 0.8 0.6 0.8 0.3 0.3 1.0 0.6
[73] 0.9 0.3 0.0 1.0 0.5 0.6 0.7 0.7 0.6 0.3 0.4 0.0 0.3 0.1 0.6 0.2 0.1 0.7
[91] 0.9 0.8 0.3 0.2 0.5 0.6 0.6 0.1 0.0 0.9 0.4 0.6 0.3 0.2 0.9 0.6 0.0 0.2
[109] 0.3 0.3 0.3 0.7 0.4 0.8 0.5 0.9 0.6 0.5 0.3 1.0 0.6 0.7 0.9 0.1 0.8 1.0
[127] 0.3 1.0 0.2 0.9 0.2 0.3 0.5 0.4 0.1 0.6 0.6 0.0 0.3 0.3 0.0 0.3 0.3 1.0
[145] 0.6 0.5 0.1 0.7 0.6 0.4
round(runif(75),1)
[1] 0.7 0.3 0.7 0.9 0.8 0.1 0.4 0.2 0.5 0.4 0.1 0.7 0.1 0.6 1.0 0.3 0.4 0.7 0.2
[20] 0.2 0.3 0.4 0.4 0.0 0.1 0.2 0.3 0.5 0.1 1.0 0.3 0.5 0.3 0.7 0.1 0.6 0.6 0.6
[39] 0.5 0.7 0.5 0.8 0.1 1.0 0.7 0.4 0.6 0.1 0.5 0.5 0.9 0.3 0.8 0.9 0.3 0.9 0.7
[58] 0.6 0.8 0.4 0.4 0.7 0.4 0.1 0.2 0.6 0.6 0.9 0.3 0.6 0.5 0.9 0.2 0.3 0.2
round(runif(75),3)
[1] 0.712 0.355 0.130 0.768 0.134 0.681 0.273 0.663 0.849 0.851 0.842 0.430
[13] 0.371 0.903 0.148 0.879 0.812 0.330 0.567 0.646 0.199 0.159 0.056 0.448
[25] 0.637 0.204 0.101 0.389 0.797 0.030 0.021 0.167 0.440 0.359 0.670 0.435
[37] 0.807 0.669 0.738 0.546 0.535 0.969 0.055 0.201 0.436 0.336 0.841 0.548
[49] 0.901 0.850 0.369 0.770 0.678 0.922 0.252 0.132 0.635 0.544 0.291 0.715
[61] 0.601 0.399 0.585 0.161 0.423 0.244 0.451 0.397 0.951 0.382 0.123 0.959
[73] 0.252 0.330 0.238

Updated on: 06-Nov-2020

3K+ Views