How to sample from a user-defined function that generates random numbers

Rebecca J. Stones

I have written a function x <- function() ... that generates random numbers according to a distribution I'd like to study.

> x()
[1] 0.8947771
> x()
[1] 0.4478619

I can generate a list of 10 random numbers using x via a for loop:

> s <- c()
> for(i in 1:10) s <- c(s,x())
> s
 [1] 0.6035317 0.4556456 0.6063270 0.4567958 0.5805186 0.7688124 0.6722493
 [8] 0.3908357 0.4513608 0.2747064

However, this seems clumsy. I would like to know: is there a better way to create such a list?

There are some succinct ways of generating lists numbers in R, such as (1:10)^2 (which generates squares) and runif(10,0,1) (which generates uniformly random numbers between 0 and 1), rnorm(10) (which generates normal-distributed numbers) but I can't seem to get it to work in my case: x(1:10) returns Error in x(1:10) : unused argument(s) (1:10).

Jilber Urbina

Try using replicate

replicate(10, x())

Also note that you are in Circle 2, read R inferno

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sample random numbers of particular variance from truncated normal distribution?

From Dev

How to do an addition from random numbers by the user?

From Dev

Matlab function that generates random real numbers in a closed interval

From Dev

Using while loop to repeat a function that generates random numbers

From Dev

Function in bash that generates random chars from /dev/random

From Dev

Function in bash that generates random chars from /dev/random

From Dev

How to call user defined function from another user defined function in bash?

From Java

How to create a function that generates an infinite list of an integer-sequence that is defined recursively with two initial numbers X_0 and X_1

From Dev

Armadillo generates the same random numbers

From Dev

Generates random numbers with 11 digits

From Dev

Python: random function from locals() with defined prefix

From Dev

How to rename a user defined function

From Dev

How can I code a sample of random numbers from a list, so that each number generated follows some condition depending on the preceding number?

From Dev

Random() in a user defined arraylist

From Dev

How to populate an array from size (equal to user input) with random unique numbers? CLOJURE

From Dev

How does this hash function work? Are these numbers random?

From Dev

'random_sample' is not defined when using pip

From Dev

Calling user defined function from AngularJS directive

From Dev

Query from user defined function JOOQ

From Dev

Unexpected Result from User Defined Function - PHP

From Dev

User-defined function for finding max of 4 numbers

From Dev

Random Number Generator Generates Same Numbers on Android

From Dev

How to get a random (bootstrap) sample from pandas multiindex

From Java

How to sample random variable from a different sized probability distribution

From Dev

Marklogic: How to return a set of items from a sequence randomly (random sample)?

From Dev

Array#sample random numbers generator

From Dev

How to create a User Defined Function for SQL server

From Dev

How to write mysql user defined function in php

From Dev

How to define user defined function in pandas

Related Related

  1. 1

    How to sample random numbers of particular variance from truncated normal distribution?

  2. 2

    How to do an addition from random numbers by the user?

  3. 3

    Matlab function that generates random real numbers in a closed interval

  4. 4

    Using while loop to repeat a function that generates random numbers

  5. 5

    Function in bash that generates random chars from /dev/random

  6. 6

    Function in bash that generates random chars from /dev/random

  7. 7

    How to call user defined function from another user defined function in bash?

  8. 8

    How to create a function that generates an infinite list of an integer-sequence that is defined recursively with two initial numbers X_0 and X_1

  9. 9

    Armadillo generates the same random numbers

  10. 10

    Generates random numbers with 11 digits

  11. 11

    Python: random function from locals() with defined prefix

  12. 12

    How to rename a user defined function

  13. 13

    How can I code a sample of random numbers from a list, so that each number generated follows some condition depending on the preceding number?

  14. 14

    Random() in a user defined arraylist

  15. 15

    How to populate an array from size (equal to user input) with random unique numbers? CLOJURE

  16. 16

    How does this hash function work? Are these numbers random?

  17. 17

    'random_sample' is not defined when using pip

  18. 18

    Calling user defined function from AngularJS directive

  19. 19

    Query from user defined function JOOQ

  20. 20

    Unexpected Result from User Defined Function - PHP

  21. 21

    User-defined function for finding max of 4 numbers

  22. 22

    Random Number Generator Generates Same Numbers on Android

  23. 23

    How to get a random (bootstrap) sample from pandas multiindex

  24. 24

    How to sample random variable from a different sized probability distribution

  25. 25

    Marklogic: How to return a set of items from a sequence randomly (random sample)?

  26. 26

    Array#sample random numbers generator

  27. 27

    How to create a User Defined Function for SQL server

  28. 28

    How to write mysql user defined function in php

  29. 29

    How to define user defined function in pandas

HotTag

Archive