How do you save the generated random numbers in R language?

In R, you can use the function set.seed() to set a seed, then use the functions runif() or rnorm() to generate random numbers, and finally use the function save() to save the generated random numbers to a file.

Here is an example code for saving generated random numbers:

# 设置种子
set.seed(123)

# 生成随机数
random_numbers <- runif(10)

# 保存随机数到文件
save(random_numbers, file = "random_numbers.RData")

In the above code, the set.seed(123) function is first used to set the seed to 123, then the runif(10) function is used to generate 10 random numbers between 0 and 1, which are saved in a variable called random_numbers. Finally, the save() function is used to save random_numbers to a file named random_numbers.RData.

Note: The saved file is in R’s binary format. If you need to save it in another format, you can use the corresponding functions for conversion, such as write.csv() for saving in CSV format, write.table() for saving in text format, etc.

bannerAds