Mathematica, increasing by 2n+1, not, say 3
Here's what I have.
RandomChoice[{.5, .5} -> {Heads, Tails}, 3]
then I write
RandomChoice[{.5, .5} -> {Heads, Tails}, 5]
How do I increase this function by 开发者_Python百科increments of 2n+1?
I interpreted your request as indicating you wanted the function, 2n + 1, with increments of 2. So the values would be: 3, 5, 7, 9, 11...If that is the correct interpretation then the following should work:
Table[RandomChoice[{heads, tails}, 2 n + 1], {n, 1, 10}]
So, f(n) = 2 n + 1 for the domain of {1,2,3,4,5,6,7,8,9,10}
You can achieve the same result with:
Table[RandomChoice[{heads, tails}, n], {n, 3, 21, 2}]
This makes it even clearer that the increment is 2. However it is not the same function. The target is the same as before but the domain is now the same as the target set: {3, 5, 7...21}
Is something like this what you're looking for?
For[i = 1, i < 5, i++, Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]]]
Which would give a sample output of:
{Heads, Tails, Tails}
{Tails, Tails, Heads, Heads, Tails}
{Tails, Tails, Heads, Tails, Heads, Heads, Tails}
{Heads, Heads, Tails, Tails, Heads, Tails, Heads, Tails, Tails}
This also does the same thing:
Do[Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]], {i, 4}]
I don't know if there is a more elegant/efficient way of doing this, since I'm new to Mathematica - but I'm sure someone will comment if there is.
精彩评论