Game Development Reference
In-Depth Information
be part of the range, however, use I]pd*nkqj` instead. I]pd*nkqj`$I]pd*n]j`ki$%&-,,% will give
you a random number between 0 and 100.
What if you want to generate a random number within a range of numbers that starts
at something other than 1 or zero?
Let's say you need a number between 10 and 25. That means that you have 15 possible
numbers that could be chosen: 10, 11, 12 . . . up to 25. All you need to do is generate
a random number between 0 and 15, and then add 10 to it push it up to within the
range you need. This is what the code will look like:
I]pd*nkqj`$I]pd*n]j`ki$%&-1%'-,
Think about it this way. The random number is between 0 and 15. Let's say it's 8. Then
you add 10 to it. You end up with 18. You've got a range of possible random numbers
between 10 and 25!
Now use what you learned about random numbers and apply it to the game:
1. Modify the ejep method with the new code in bold. In addition to randomizing the
iuopanuJqi^an variable, you've added a pn]_a directive for testing purposes so that you can
actually see what that number is in the Output panel. (Notice that string concatenation was
used in the pn]_a directive so that the display in the Output panel is more readable.)
bqj_pekjejep$%6rke`
w
++Ejepe]hevar]ne]^hao
op]npIaoo]ca9E]ipdejgejckb]jqi^an^apsaaj-]j`-,,7
iuopanuJqi^an9I]pd*_aeh$I]pd*n]j`ki$%&-,,%7
cqaooaoNai]ejejc9-,7
cqaooaoI]`a9,7
c]iaOp]pqo97
c]iaSkj9b]hoa7
++Pn]_apdaiuopanujqi^an
pn]_a$Pdaiuopanujqi^an6'iuopanuJqi^an%7
++Ejepe]hevapatpbeah`o
kqplqp*patp9op]npIaoo]ca7
ejlqp*patp97
ejlqp*^]_gcnkqj`?khkn9,tBB??????7
ejlqp*naopne_p9,)57
op]ca*bk_qo9ejlqp7
++=``]jarajpheopajanpkpda^qppkj
cqaoo>qppkj*]``ArajpHeopajan$IkqoaArajp*?HE?G(kjCqaoo>qppkj?he_g%7
y
 
Search Nedrilad ::




Custom Search