- Home /
how do i implement probability in java script
i was wonder how i would use probability in script for unity preferably java. some examples like: the probability the Ai soldir in the game will see your player when he's wearing camo. like how would i use it so there is a 60 percent chance the Ai will spot you when a bool prop is true or say a card game like poker and getting random cards. any type of explination will be appreciated preferably a example for each typed in script
Answer by BinaryCaveman · Jan 02, 2011 at 11:46 PM
You would want to generate a random number, using Random.Range(min, max)
- (min
is inclusive, max
is exclusive). Read more here.
A simple example for the soldier situation could be:
var soldierCanSeeYou : bool = true;
// if prop is true and a random number between 0 and 100 is less than 60 (60% chance) if (soldierCanSeeYou && Random.Range(0, 100) <= 60) { // Soldier has spotted you (run!!!) }
another for the cards example:
function getRandomCard() {
var cards = new Array( '1', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A' ); // an array containing all the possible types of cards to get
return cards[Random.range(0, cards.length)]; // use the Random.Range() function again to get a random card in the array and return it
}
good job! but it's in c# (even though its extremely easy to convert to unityscript.) just saying to anyone who doesnt know scripting, this is a c# example.
As far as I know, this is JavaScript, UnityScript, whatever you want to call it.
Your answer
Follow this Question
Related Questions
random target selection 2 Answers
AI for enemy characters 1 Answer
Top Down Shooter Enemy AI using raycasting 1 Answer
Need help with javascript AI 1 Answer
Enemy AI Problem: Dancing? 1 Answer