Ask Your Question

How does one get Sage to make random choices?

asked 2015-02-09 15:54:11 -0500

phoenix gravatar image

Like say in the middle of a code I compute a function value $f(x)$ and I knwo that $0 \leq f(x) \leq 1$. Now I want to write an if-then-else which will do one option with probability $f(x)$ and the other otion with probability $1-f(x)$. How does one make Sage do this?

Can somoen kindly show a template example to simulate this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-09 16:42:15 -0500

tmonteil gravatar image

updated 2015-02-09 16:48:38 -0500

This question looks like homework, so it could be nice to tell us where is the point you are stuck. Here are some hints: if you want to pick a number between 0 and 1 uniformly at random, just use the random() function:

sage: random()

Now, what you have to do is to compare this value to f(x). For this, you have to know that Sage uses the Python programming language, so you can look for tutorials about "if statement" in Python.

Please tell us if you encounter difficulties (and do not hesitate to post your code and where it does not work), so that we can help further.

edit flag offensive delete link more


@tmonteil I don't know in what kind of a course can this be a homework :D I am a researcher who recently started using Python and Sage for my research! Back to the question : I don't understand what you are saying. Roughly I would think that one needs to simulate a coin toss where heads come with probability $f(x)$. How is that equivalent to comparing $f(x)$ to a randomly generated number in the interval $[0,1]$ ? (If the two options are $a$ and $b$ then say I want to choose $a$ with a probability $f(x)$ and $b$ with a probability $1-f(x)$ and then I guess you mean that this is equivalent to choosing $a$ if the random number is (greater? or lesser?) then $f(x)$ or else otherwise?) I am not getting you.

phoenix gravatar imagephoenix ( 2015-02-09 20:46:36 -0500 )edit

Yes, i mean that the following are equivalent:

  • to choose a with probability f(x) and b otherwise (hence with probability 1-f(x))
  • to pick a random number y in [0,1] and choose a if y<f(x) and b if y>=f(x)

To get convinced, draw a picture of the interval [0,1] cut into two intervals [0,f(x)) and [f(x),1]: the first one has length f(x), the second has length 1-f(x). When you pick a random number y in [0,1] (according to Lebesgue measure), the probability that y belongs to [0,f(x)) is f(x) and the probability that y belongs to [f(x),1] is (1-f(x)) (indeed, on [0,1], the Lebesgue measure is nothing but the length).

tmonteil gravatar imagetmonteil ( 2015-02-09 22:39:44 -0500 )edit

Thanks! Somehow I got confused!

phoenix gravatar imagephoenix ( 2015-02-10 11:52:32 -0500 )edit

If you are happy you can choose @tmonteil answer by clicking on the button "accept" on the left!

vdelecroix gravatar imagevdelecroix ( 2015-02-14 06:01:27 -0500 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2015-02-09 15:54:11 -0500

Seen: 123 times

Last updated: Feb 09 '15