Working With Random Number Generation

As much of a tool as an “if statement” or a “for loop” can be for you, generating random numbers can be equally as useful and equally as versatile.

You can use random numbers to create percentage-based occurrences such as loot drop ratios, enemy spawn rates, crit-chances, AI decision making, Procedural Generation, etc.

Here’s an example of how to use random number generation, and track it based on a percentile ratio.

I set range increments of 25 but you can add any amount of different ranges that you feel meet your needs.

You can use this with “if statements” or with “switch statements”.

This code sequence could be tied to a games looting system. You could give each different item a number range that is associated with its drop chance and for loop the sequence a number of times equal to the number of items you’d like to spawn.

Or you could use it to determine various states of behavior for an AI. Say you wanted your enemies to change the way that they move based on a random chance. This can create an elaborate and more immersive feeling AI, and can very easily be expanded upon.

[SIDE NOTE]

Don’t forget that when declaring a range of numbers, that the maximum number that you will get is one less than the maximum number value that you typed since the system begins counting from 0. This is why a percentile range would look like (0, 100) and not (1, 100) which would only result in a 1 - 99 number range. This works similarly for Arrays as well.

--

--

--

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why beginners should start programing with the ‘Google Colab’?

CS373 Fall 2020: Michael Liu

https://youtu.be/nh8B7AASqA4

Should you use .map(), .reduce(), or .filter()? Are they fast enough?

The Software Architect Elevator by Gregor Hohpe

MeetUp — A Flutter App to manage all your online meetings in one place

CSS Variable: Declare variable

Exploring Design Patterns — Decorator Pattern

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Reed

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

More from Medium

2D Light Flicker in Unity

Variables! — The building blocks of programming

Journal 139 — Unity Project Development Part 1–2.5D Shooter

Adding a burst laser powerup