As a language for statistical analysis, R has a comprehensive library of functions for generating random numbers from various statistical distributions. In this post, I want to focus on the simplest of questions: How do I generate a random number? If you want to generate a decimal number where any value (including fractional values) between the stated minimum and maximum is equally likely, use the runif function. This function generates values from the Uniform distribution. Here’s how to generate one random number between 5.0 and 7.5: Of course, when you run this, you’ll get a different number, but it will definitely be between 5.0 and 7.5. You won’t get the values 5.0 or 7.5 exactly, either. If you want to generate multiple random values, don’t use a loop. You can generate several values at once by specifying the number of values you want as the first argument to runif. Here’s how to generate 10 values between 5.0 and 7.5: This looks like the same exercise as the last one, but now we only want whole numbers, not fractional values. For that, we use the sample function: The first argument is a vector of valid numbers to generate (here, the numbers 1 to 10), and the second argument indicates one number should be returned. If we want to generate more than one random number, we have to add an additional argument to indicate that repeats are allowed: Note the number 6 appears twice in the 5 numbers generated. (Here’s a fun exercise: what is the probability of running this command and having no repeats in the 5 numbers generated?) If you wanted to simulate the lotto game common to many countries, where you randomly select 6 balls from 40 (each labelled with a number from 1 to 40), you’d again use the sample function, but this time without replacement: You’ll get a different 6 numbers when you run this, but they’ll all be between 1 and 40 (inclusive), and no number will repeat. Also, you don’t actually need to include the replace=F option — sampling without replacement is the default — but it doesn’t hurt to include it for clarity. You can use this same idea to generate a random subset of any vector, even one that doesn’t contain numbers. For example, to select 10 distinct states of the US at random: … but sampling exactly the number you do have is a great way to randomize the order of a vector. Here are the 50 states of the US, in random order: You could also have just used sample(state.name) for the same result — sampling as many values as provided is the default. For more information about how R generates random numbers, check out the following help pages: runif, and sample. .Random.seed provides technical detail on the random number generator R uses, and how you can set the random seed to recreate strings of random numbers. For a more information on random number generators and True random number generators, the vignette for the ‘accuracy’ package is worth looking at. Hello, could you please advise me how to generate correlated random numbers for a single vector? Thanks in advance for your help. This section includes tips and tricks about R contributed by R users. Add your own suggestion, tips, and ‘How to…’ guides for R by logging in to Inside-R.org. User contributions to this section are contributed under a cc-wiki license with attribution (to Inside-R.org and the contributing user) required. inside-R.org is a collection of resources about the open-source R Project for the R Statistics Community. This site is sponsored by Revolution Analytics. Source.