Random number generation

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Random number generation

karena
Hi,

I want to generate multiple sets of random numbers.
The requirement is that:
1) each set have 3 random numbers;
2) the sum of the three number is always 1.

how to do this?

thank you,

karena
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

Jason Edgecombe
On 07/21/2011 07:18 PM, karena wrote:
> Hi,
>
> I want to generate multiple sets of random numbers.
> The requirement is that:
> 1) each set have 3 random numbers;
> 2) the sum of the three number is always 1.
>
> how to do this?

Try this:

     df<-data.frame(a=runif(10),b=runif(10))
     df$c=df$a+df$b
     df

Replace 10 with the number of rows that you want. Replace runif with
your random function of choice.

Jason

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

Jason Edgecombe
On 07/21/2011 08:04 PM, Jason Edgecombe wrote:

> On 07/21/2011 07:18 PM, karena wrote:
>> Hi,
>>
>> I want to generate multiple sets of random numbers.
>> The requirement is that:
>> 1) each set have 3 random numbers;
>> 2) the sum of the three number is always 1.
>>
>> how to do this?
>
> Try this:
>
>     df<-data.frame(a=runif(10),b=runif(10))
>     df$c=df$a+df$b
>     df
>
> Replace 10 with the number of rows that you want. Replace runif with
> your random function of choice.
>
> Jason
Nevermind, I didn't address how to ensure that each row adds up to one.

Sorry,
Jason

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

Duncan Murdoch-2
In reply to this post by karena
On 21/07/2011 7:18 PM, karena wrote:
> Hi,
>
> I want to generate multiple sets of random numbers.
> The requirement is that:
> 1) each set have 3 random numbers;
> 2) the sum of the three number is always 1.
>
> how to do this?

There are lots of ways.  What is the context?  It would determine the
first two steps:

1.  Generate A.
2.  Generate B.
3.  C <- 1-A-B

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

Rolf Turner-3
In reply to this post by karena

Could this possibly be a homework question?

     cheers,

         Rolf Turner

On 22/07/11 11:18, karena wrote:
> Hi,
>
> I want to generate multiple sets of random numbers.
> The requirement is that:
> 1) each set have 3 random numbers;
> 2) the sum of the three number is always 1.
>
> how to do this?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

karena
In reply to this post by Jason Edgecombe
To Jason: Still thank you, your reply gives me some clue about how to do this.

To Duncan: Thank you for rely. It helped.

Karena
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

djmuseR
In reply to this post by karena
Hi:

Here's one way using uniform(0, 1) pseudo-random numbers, but there
are many ways you could go about this.

# each row comprises a set of three pseudo-random numbers
u <- matrix(runif(30), nrow = 10)
# divide each element in a row by its row sum
v <- t(apply(u, 1, function(x) x/sum(x)))
rowSums(v)
 [1] 1 1 1 1 1 1 1 1 1 1

# An equivalent way (about equally fast) is
u/outer(rowSums(u), rep(1, 3))

Now try

hist(unlist(v))

and notice that the distribution of the constrained sets is not really
uniform. This is a consequence of setting a constraint on the sum of
each sample. Another way to see this is to plot

plot(sort(unlist(v)))

A 'truly' uniform random sample would lie approximately on a straight
line in this plot.

It would seem to me that a better approach would be to sample from a
simplex embedded in the unit cube. I'd suggest looking into the
compositions package (because you are effectively generating
compositional data) and look into its capabilities. At least a couple
of the references in the package's overview page seem to be germane to
the problem.
The pair of runif.* functions appear to be relevant.

HTH,
Dennis



On Thu, Jul 21, 2011 at 4:18 PM, karena <[hidden email]> wrote:

> Hi,
>
> I want to generate multiple sets of random numbers.
> The requirement is that:
> 1) each set have 3 random numbers;
> 2) the sum of the three number is always 1.
>
> how to do this?
>
> thank you,
>
> karena
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Random-number-generation-tp3685463p3685463.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

Daniel Nordlund-4
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Dennis Murphy
> Sent: Thursday, July 21, 2011 8:55 PM
> To: karena
> Cc: [hidden email]
> Subject: Re: [R] Random number generation
>
> Hi:
>
> Here's one way using uniform(0, 1) pseudo-random numbers, but there
> are many ways you could go about this.
>
> # each row comprises a set of three pseudo-random numbers
> u <- matrix(runif(30), nrow = 10)
> # divide each element in a row by its row sum
> v <- t(apply(u, 1, function(x) x/sum(x)))
> rowSums(v)
>  [1] 1 1 1 1 1 1 1 1 1 1
>
> # An equivalent way (about equally fast) is
> u/outer(rowSums(u), rep(1, 3))
>
> Now try
>
> hist(unlist(v))
>
> and notice that the distribution of the constrained sets is not really
> uniform. This is a consequence of setting a constraint on the sum of
> each sample. Another way to see this is to plot
>
> plot(sort(unlist(v)))
>
> A 'truly' uniform random sample would lie approximately on a straight
> line in this plot.
>
> It would seem to me that a better approach would be to sample from a
> simplex embedded in the unit cube. I'd suggest looking into the
> compositions package (because you are effectively generating
> compositional data) and look into its capabilities. At least a couple
> of the references in the package's overview page seem to be germane to
> the problem.
> The pair of runif.* functions appear to be relevant.
>
> HTH,
> Dennis
>
>
>
> On Thu, Jul 21, 2011 at 4:18 PM, karena <[hidden email]> wrote:
> > Hi,
> >
> > I want to generate multiple sets of random numbers.
> > The requirement is that:
> > 1) each set have 3 random numbers;
> > 2) the sum of the three number is always 1.
> >
> > how to do this?
> >
> > thank you,
> >
> > karena
> >

Karena,

if you search the R-help archive you will find a couple of threads discussing this topic.  Here is one URL to get you started.

  http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2520.html

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Random number generation

karena
Thank you guys for all the help. I appreciate!