How to generate a random number with R

Source: Internet
Author: User
Tags number strings random seed repetition

Reproduced from How to use R to generate a random number

How to select a random number in R

As a language for statistical analysis, R have a comprehensive library of functions for generating random numbers from Vari OUs statistical distributions. As a language for statistical analysis, R has a random number to generate a variety of statistical distribution functions of the comprehensive library. In this post, I want to focus on the simplest of questions:how does I generate a random number? In this article, I want to concentrate on simple questions: How do I generate a random ?

The answer depends on what kind's random number you want to generate. The answer depends on how random numbers you want to generate. Let's illustrate by example for example.
Generate a random number between 5.0 and 7.5 generates an arbitrary count between 5.0 and 7.5 If you are want to Generate a, where any value (in cluding fractional values) between the stated minimum and maximum are equally likely, use the runif function. If you want to generate a decimal number rule Any value between the lowest and highest (including fractional values) is also possible, using the runif function. This functions generates values from the Uniform distribution. The function generates a uniformly distributed value. Here's how to generate one, random number between 5.0 and 7.5: Here's a way to generate random numbers between 5.0 and 7.5:

> x1 <-runif (1, 5.0, 7.5) > X1 <-runif (1,5.0,7.5)
> x1 > X1
[1] 6.715697 [1] 6.715697

Of course, when u run this, you'll get a different number, but it'll definitely be between 5.0 and 7.5. Of course, when you run this, you get a A different number, but it is bound to be between 5.0 and 7.5. You won ' t get the values 5.0 or 7.5 exactly and either. You won't get the exact value 5.0 or 7.5.
If you want to generate multiple random values, don't use a loops. If you want to generate more than one random value, do not take a loop. You can generate several values at once by specifying the number of values in your want as the first argument to runif. You can generate multiple One value at a time by specifying the number of values you want to runif as the first parameter. Here's how to generate the values between 5.0 and 7.5: Here's how to produce a value between 10 5.0 and 7.5:

> x2 <-runif (Ten, 5.0, 7.5) > 2 <-runif (10,5.0,7.5)
> x2 > 2
[1] 6.339188 5.311788 7.099009 5.746380 6.720383 7.433535 7.159988 [1] 6.339188 5.311788 7.099009 5.746380 6.720383 7.4335 35 7.159988
[8] 5.047628 7.011670 7.030854 [8] 5.047628 7.011670 7.030854

Generate a random integer between 1 and 10 generates a 1 to 10
This looks likes the same exercise as the last one, though now we are only want whole numbers, not fractional values. It looks like the final of the same op But now we only want the complete number, not the fractional value. For this, we use the sample function: To do this, we used the example functions:

> x3 <-sample (1:10, 1) > X3 <-sample (1:10,1)
> x3 > X3
[1] 4 [1] 4

The first argument is a vector of valid numbers to generate (here, the numbers 1 to ten), and the second argument indicates One number should be returned. The first parameter is a valid number vector generation (here the number 1 to 10), and the second parameter indicates that a digit should be returned. If we want to generate more than one random number, we had to add a additional argument to indicate that repeats is all Owed: If we are going to generate multiple random numbers, we have to add an extra parameter that allows for repetition:

> x4 <-sample (1:10, 5, replace=t) x4> <-sample (1:10,5, replace = T)
> x4 > X4
[1] 6 9 7 6 5 [1] 6 9 7 6 5

Note The number 6 appears twice in the 5 numbers generated. Notice that numbers 6th generated in 5 appeared two times.  (Here's a fun exercise:what are the probability of running this command and have no repeats in the 5 numbers generated?) (Here's an interesting exercise: run this command, and the number generated in 5 has no repetition probability of what?) )
Select 6 random numbers between 1 and, without replacement Select a number between 6 1 and 40, no replacement required
If you wanted to simulate the lotto game common to many countries, where you randomly select 6 balls from + (each LaBelle D with a number from 1 to +), you ' d again used the sample function, but this time without replacement: if you want to simulate a lotto game, often see many countries, Where you randomly select 6 balls from 40 (each with a digital marker from 1 to 40), you will use it again without changing the sample function, but this time:

> x5 <-sample (1:40, 6, replace=f) x5> <-sample (1:40 6, replace = F)
> x5 > X5
[1] 10 21 29 12 7 31 [1] 10 21 29 12 7 31

You'll get a different 6 numbers when you run this, but they ' ll is between 1 and (inclusive), and no number would re Peat. When you run this, you will get a different 6 numbers, but they will all be between 1 and 40 (inclusive), and no number will be duplicated. Also, you don ' t actually need to include thereplace=fOption-Sampling without replacement is the default – but it doesn ' t hurt to include it for clarity. In addition, you do not actually need to includeReplace = FOption-No replacement sampling is the default-but it does not hurt including its sharpness.
Select ten items from a list of 50 selected 10 projects from 50 single
You can use this same ideas to generate a random subset of any vector, even one, doesn ' t contain numbers A random subset of what vectors, even without numbers. For example, to select ten distinct states of the US at random: for example, select 10 different US states randomly:

> Sample (State.name, ten) > Samples (state.name)
[1] "Virginia" "Oklahoma" "Maryland" "Michigan" 1] "Virginia", "Oklahoma", "Maryland", "Michigan"
[5] "Alaska" "South Dakota" "Minnesota" "Idaho" 5] "Alaska", "South Dakota", "Minnesota", "Idaho"
[9] "Indiana" "Connecticut" [9] "Treasure", "Connecticut"

You can ' t sample greater values than you had without allowing replacements: you can't taste more value than you've ever allowed to replace:

> Sample (state.name) > Samples (state.name)
Error in sample (State.name, 52): Sampling error (state.name,52 years):
Cannot takes a sample larger than the population when ' replace = False ' cannot take a larger population of samples when "Replace = false"

... but sampling exactly the number for you does has a great way to randomize the order of a vector. But completely sampled, you had a great manner of random vectors of Order. Here is the states of the US, in random order: The following are the random orders in the 50 states:

>sample (State.name, +)>sample (State.name)
[1] "California" "Iowa" "Hawaii" 1] "California", "Hwa", "Hawaii"
[4] "Montana" "South Dakota" "North Dakota" [4] "Montana" South Dakota "," North Dakota State "
[7] "Louisiana" "Maine" "Maryland" 7] "Louisiana", "Maine", "Maryland"
[Ten] "New Hampshire" "Rhode Island" "Texas" [10] "Rhode Lsland", "Texas"
[] "Florida" "North Carolina" "Minnesota" [13] "Florida", "Northern Carolina" "Minnesota"
[+] "Arkansas" "Pennsylvania" "Colorado" 16] "Arkansas", "Pennsylvania", "Colorado"
[+] "Idaho" "Connecticut" "Utah" 19] "Idaho", "Connecticut", "Jazz"
[] "South Carolina" "Illinois" "Ohio" [22] "South Carolina State", "Illinois State" Ohio "
[+] "new Jersey" "Indiana" "Wisconsin" 25] "New Jersey", "Treasure", "Wisconsin"
[Mississippi] "Michigan" "Wyoming" [28] "Mississippi", "Michigan" Wyoming "
[To] "West Virginia" "Alaska" "Georgia" 31] "western Virginia", "Alaska", "Georgia"
[Vermont] "Virginia" "Oklahoma" 34] "Vermont State", "Virginia", "Oklahoma"
[PNS] "Washington" "New Mexico" "New York" 37] "Washington" New Mexico "NY"
[Delaware] "Nevada" "Alabama" [40] "Delaware State" Nevada "Alabama"
[Kentucky] "Missouri" "Oregon" [43] "KFC", "Missouri" Oregon "
[Tennessee] "Arizona" "Massachusetts" [46] "Tennessee" Arizona "Massachusetts"
[Kansas] "Nebraska" [49] "Kansas", "Nebraska"

You could also has just used sample (State.name) for the same result – sampling as many values as provided is the default You also have the same result only with samples (STATE.NAME)-sampling provides many values that are default.
Further reading further reading
For more information on how to R generates random numbers, check out the "following help pages" For more information on how r generates random numbers, please check the following Help page:

> runif ; Runif
> Sample > Samples
> ?. Random.seed ;? Random.seed

The last of these provides technical detail in the random number generator R uses, and how can set the random seed to Recreate strings of numbers. These r uses the random number generator to provide technical details and how to set random seed re-random number strings. ====================================================================================================== original article is as follows: how To choose a random number in R

As a language for statistical analysis, R have a comprehensive library of functions for generating random numbers from Vari OUs statistical distributions. In this post, I want to focus on the simplest of questions:how do I generate a random number?

The answer depends on what kind's random number you want to generate. Let's illustrate by example.
Generate a random number between 5.0 and 7.5If want to Generate a decimal number where any value (including fractional Values) between the stated minimum and maximum are 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:

> x1 <-runif (1, 5.0, 7.5)
> x1
[1] 6.715697

Of course, when you run this, you'll get a different number, but it'll 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 the random values, don ' t use a loop. You can generate several values at once by specifying the number of values in your want as the first argument to runif. Here's how to generate values between 5.0 and 7.5:

> X2 <-runif (10, 5.0, 7.5)
> x2
[1] 6.339188 5.311788 7.099009 5.746380 6.720383 7.433535 7.159988
[8] 5.047628 7.011670 7.030854

Generate a random integer between 1 and 10
This looks like the same exercise as the last one, but now we are only want whole numbers, not fractional values. For this, we use the sample function:

> x3 <-sample (1:10, 1)
> X3
[1] 4

The first argument is a vector of valid numbers to generate (here, the numbers 1 to ten), and the second argument indicates One number should be returned. If we want to generate more than one random number, we had to add a additional argument to indicate that repeats is all Owed:

> x4 <-sample (1:10, 5, replace=t)
> x4
[1] 6 9 7 6 5

Note The number 6 appears twice in the 5 numbers generated. (Here's a fun exercise:what are the probability of running this command and have no repeats in the 5 numbers generated?)
Select 6 random numbers between 1 and without replacement
If you wanted to simulate the lotto game common to many countries, where you randomly select 6 balls from + (each LaBelle D with a number from 1 to +), you ' d again use the sample function, but this time without replacement:

> x5 <-sample (1:40, 6, replace=f)
> x5
[1] 10 21 29 12 7 31

You'll get a different 6 numbers when you run this, but they ' ll is between 1 and (inclusive), and no number would re Peat. Also, you don't actually need to include the replace=foption – sampling without replacement is the default – but it does N ' t hurt to include it for clarity.
Select ten items from a list of 50
You can use this same idea to generate a random subset of any vector, even one, doesn ' t contain numbers. For example, to select ten distinct states of the US at random:

> sample (State.name, ten)
[1] "Virginia" "Oklahoma" "Maryland" "Michigan"
[5] "Alaska" "South Dakota" "Minnesota" "Idaho"
[9] "Indiana" "Connecticut"

You can T-sample more values than you have without allowing replacements:

> sample (State.name, *)
Error in sample (State.name, 52):
Cannot take a sample larger than the population when ' replace = FALSE '

... but sampling exactly the number, you do have a great-to randomize the order of a vector. Here is the states of the US, in random order:

> sample (State.name,)
[1] "California" "Iowa" "Hawaii"
[4] "Montana" "South Dakota" "North Dakota"
[7] "Louisiana" "Maine" "Maryland"
[Ten] "New Hampshire" "Rhode Island" "Texas"
[] "Florida" "North Carolina" "Minnesota"
[+] "Arkansas" "Pennsylvania" "Colorado"
[+] "Idaho" "Connecticut" "Utah"
[] "South Carolina" "Illinois" "Ohio"
[+] "New Jersey" "Indiana" "Wisconsin"
[] "Mississippi" "Michigan" "Wyoming"
[To] "West Virginia" "Alaska" "Georgia"
[Vermont] "Virginia" "Oklahoma"
[PNS] "Washington" "New Mexico" "New York"
[+] "Delaware" "Nevada" "Alabama"
[] "Kentucky" "Missouri" "Oregon"
[Tennessee] "Arizona" "Massachusetts"
[Kansas] "Nebraska"

You could also has just used sample (State.name) for the same result – sampling as many values as provided is the default .
Further reading
For more information about what R generates random numbers, check out the following help pages:

runif >?
> ? sample
> ?. Random.seed

The last of these provides technical detail in the random number generator R uses, and how can set the random seed to Recreate strings of random numbers.

How to generate a random number with R

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.