Processing math: 22%
Ask Your Question
0

Generating a random vector with elements in specified range

asked 6 years ago

ds22 gravatar image

I would like to generate a random vector in which its elements are between 0 and 1 (including 0 and 1). As far as I know, v = random_matrix(ZZ, 1, 10) is a way to generate a random matrix. How would I restrict the elements to the range I want?

I am trying to build a function with a probability input, and then compare the probability to each of the elements separately in the random vector.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 6 years ago

Emmanuel Charpentier gravatar image

This returns such a random vector of a given size. However, note that its elements belong to the semi-open interval [0 1)R, not the closed interval [0 1]R :

sage: def randvect(size=4):return vector([random() for t in range(size)])
sage: randvect(3)
(0.8688082291795581, 0.28209569121849476, 0.3112164215493163)

HTH,

Preview: (hide)
link

Comments

@Emmanuel Charpentier: I really need it to be a closed interval though. This is the beginning of a function for a Monte Carlo simulation. I need to know when the input (which is a probability) is less than one.

ds22 gravatar imageds22 ( 6 years ago )
1

Well.. Pr, anyway, unless you are able to state some value p such as \Pr\left(X=1\right)=p, in which case, it is trivial to extract your components from a mixture of \delta(x-1) with weight p and the \mathrm{U}\left[O~1\right) (used above) with weight 1-p. What difference does it do if you are simulating ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 6 years ago )

And, by the way, what are you simulating ? Shouldn't you specify a bit more the (joint) law of your components ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 6 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 6 years ago

Seen: 4,904 times

Last updated: Apr 25 '18