Forum rules - please read before posting.

Random integers

I am doing some randomly generated things in my game and just wondered why a random integer (using the "SetVariable" action) with parameters "0 to 1" always returns 0. I found out that AC uses the Unity method "Random.Range" which excludes the "max" value. E.g. a max value of 10 given in the action will return numbers from 0 to 9. This should be mentioned somewhere or even better AC should work with an "entered value+1" when generating the random integer.

From the Unity scripting API:
public static int Range(int min,
int max);



Description

Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).


Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9.
If max equals min, min will be returned.




Comments

  • edited December 2016
    Umm... Unity's Random.Range has both min and max "inclusive" for float, are you certain the AC's action is using intergers? I've never really checked (as all it needs is add +1 to the max number or using floats, or making sure min is always 0). Still, do be careful using randomized numbers, they're a pain in the ass to handle, be them Unity's or System's. remember that those methods do trickery to get a random number, basically forming a selection pattern to then present the numbers and give the illusion that you are getting random numbers. Problem with this is that the pattern will tend be the same every time you enter playmode, if you've use the same numbers. So it will tend to give you the same selection of random numbers that you got in the last playthrough. People try to deal with this by changing the seed constantly in those classes (to things like system time or stuff), but even that may not always work as expected or cause an overhead, etc... So, anyway, just be sure you test whatever you are firing with that plenty enough to be sure it really is giving both numbers, if you just test once or twice then stop playback and try again you might never really see a difference...
  • Yes, you're right. Random numbers generated by a computer are never random. It's not a AC or Unity thing. And it's quite confusing that Unity handles float randoms with the max value included and the integer randoms with the max value excluded. AC is using the Unity integer way with its own integer variables and the max value given is never used. In spite of that I am satisfied with the "randomness" it gives.
    I added 1 to all max values in game now and it works. But I think it should be mentioned in the manual or added by 1 internally by AC when working with integers because otherwise it can lead to unnecessary bug hunting like in my case.
  • Changing the entered values would leave to confusion by those already aware of the way Unity excludes/includes them, and would also affect existing Actions already in use.  I agree that a note should be made in the Manual, however.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.