Home Artists Posts Import Register

Content

I've used a lot of random numbers in my time - and it's probably one of the most used Math operations (other than Math.floor) but it's not always optimal.

Basically Math.random pulls from a specified number and gives you one of the numbers between starting from 0. You can attach this to any form of things from displaying a list of random strings, to working it into a tick event to give something a, say, 1/10 chance of happening per specified tick.

To give a brief example of the string method, you can easily work with it like so:

var randomNum;
randomBtn.addEventListener(MouseEvent.CLICK, btnClick);
function btnClick(e:MouseEvent):void
{
randomNum = Math.floor(Math.random()*3);
if(randomNum == 0){
speech_bub._txt.text = "I am string 1";
}else if(randomNum == 1){
speech_bub._txt.text = "I am string 2";
}
ect, ect...
}


This basically gives it a simple case for it to run through. Obviously this would be better made as an actual case statement, but this works just fine for smaller operations. When you start making larger projects you'll need to make case statements - though we'll cover that at a later date.

The next thing to explain is the "Math.floor(Math.random()*3);" part that may seem rather confusing.
It gets more confusing too, because when the random number is chosen the first number starts at 0 and not 1, so the number you've put as the highest number to count to (in this case ours is 3), isn't actually what it counts up to. It instead counts up to 2, using 0 as our first number instead of 1.
So if you put 10 it's still counting from 0 - 9, or 10 numbers, but it won't count the number 10.

What's more is that this method is, as it says on the tin, completely random. I mean... it's not completely random. It's generated to be random, but true randomness is actually very difficult.
Anyway, this means that if you have a function, animation, string, ect tied to a number (lets say 5 with a maximum of 10) then the number will be in the pool the next time the tick occurs and you need to calculate a new number. I've personally seen the 1/10 number happen about 12 times in a row - which is highly improbable but not impossible.
In these events theres a few things you can do to mitigate possible repeating functions:

You can temporarily pause or stop the timer, but this may interfere with your other functions relying on this timer.
You can also add a last chosen number and then you can match that number in the last chosen number to the number you want to omit so when the same number rolls by again you can set it so if both the last chosen number and the current random number are the same it instead returns nothing and then the tick continues. This method is great for smaller projects, and you can add different layers to it to prevent the number from occurring a large number of times.

What you can ALSO do is add a way to create a secret function, and do a check to see if a random number is the same, lets say, 3 times in a row.
In theory you could create a reference array filled with the number you cant to detect then create another, empty array that you can then put things in. Like so:
var randomRefArr:array = [3,3,3];
var randomArr:array = [0,0,0];

From there you can then add numbers to it, only if the number is over 0.
You can then set up an if statement to match the two arrays in whatever timer/tick setup you've got like so:
"if(randomArr == randomRefArr)"
...or something to that degree.

There ARE downsides to this - as mentioned above - like sometimes if you're working with a smaller number pool it can be rather hard to know if the program is running randomly, and sometimes you'll have to wait a couple minutes for your number or they'll happen one after the other multiple times in a row.
But that's just probability and randomness, really.

Working with random numbers happens quite a lot, usually. It's usually what goes on behind the scenes to give you a number between one and the other - like in Minecraft when you kill a mob and one drops zombie flesh while the other drops a carrot, or like in Stardew Valley when you destroy a rock on your farm and you have a random chance to get a geode.
Of course these use slightly more advanced random number generators, but it's basically the same core premise.


I know this was kind of a rambly post, but I really hope you learnt something! If you have any questions or if you want a more practical example of random numbers let me know :) I'm far from being a programming expert, but if there's any help you need I'll try my best to help.

Comments

No comments found for this post.