Get some random numbers within a range of numbers.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
css
js
README.md
index.html

README.md

randomNumberGenerator

Get a specified amount of random numbers within a range of 0 to n, using raw JavaScript--jquery or any other JavaScript framework was not used.

To use, simply import the randomNumberGenerator script in your <head> tag:

<script src="js/randomNumberGenerator.js"></script>

And call the randomNumberGenerator() function somewhere, supplying the amount of numbers you want back (aka, the length of the returned array), and the highest number in the range, i.e,:

randomNumberGenerator(3, 28);

...attached to a button, will return an array of random numbers each time it is clicked:

[0,7,4]
[15,17,3]
[1,4,26]

This is useful for gaming scripts that require 1 or more random numbers for AI and the like, especially when a random positions in an array/arrays are required. I currently use a form of this function in the statesGame and workoutRandomizer repos.

See it in action here

Some improvement ideas:

  • Ability to pass in arrays, array lengths, string lengths, etc., rather than just numbers
  • Option to have an object returned, with key/value pairs, for some flexibility
  • Code optimization