armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# Find in which range number is between 4 possibilities

## Question

Working on a Behavior system for my AI, the script assigns a random percentage(float) to each behavior, up to a total of 100. I want to combine 2 random behaviors which an individual can have, and make him act based on that. I know how to compare 2 numbers, but how would I go about comparing 2 different behavior values to see which each of them are assigned as from the 4 possibilities, so I could set their

s to true?  I tried comparing the first behavior number between any of the possibilities, but the problem is there are of course going to be more than 2 in the range.

This is my code so far :

What is a good way of doing this? Thanks in advance, I found some answers but none of them are quite related to my issue.

## Recommended Posts

• 1

In that case, you could make list of your points, then use list.Sort(), shuffle the array of traits and assign the values. JS example:

class Trait

{

var Name : String;

var percent : int;

}

var traits : Trait[];

function Start()

{

var percentList : ArrayList = new ArrayList();

var percentLeft : int = 100;

var i : int;

for(i = 0; i < traits.Length; i++)

{

if(i < traits.Length - 1)

{

var val = Random.Range(0, percentLeft);

percentLeft -= val;

continue;

}

}

percentList.Sort();

percentList.Reverse();

Shuffle(traits);

for(i = 0; i < traits.Length; i++)

{

traits.percent = percentList;

}

}

function Shuffle(t : Trait[])

{

//look at simple blackjack in the scripts section for an example of this function

}

##### Share on other sites

• 0

So your assigning values and you want to assign the top 2 values -corresponding Boolean-? Is that what your wanting? I'm a bit confused on the question

##### Share on other sites
• 0

Question tho. Why not make an array of all traits, Then just simply shuffle the list. First two in the array/list is your chosen trait

##### Share on other sites
• 0

There is a possibility of x% for each of the traits to be chosen. I want to chose 2 traits out of the 4 based on the possibility. I will try the array shuffle as soon as I am home.

##### Share on other sites
• 0

It's not letting me put in the setting loop on here for some reason

##### Share on other sites
• 0

Now I see there are a lot of possibilities, and I have decided that since there are a lot of npc's in the scene at once, I will use a shuffle array method, so I don't need to write 120 lines of code for calculations of something that can be done easier. However, I will use the script you wrote in making more complex mechanics in the game that will control things like economy, etc... . Thanks for the help!

• 0

Np, anytime

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.