Scrappy Random

Scrappy Random is a simple helper library for Solar2D that allows for working in a random fashion, such as generating random numbers, strings, colours, and UUIDs, as well as selecting from a list and flipping a coin.

Using identical seeds it will generate the same results cross-platform.

As with all my libraries it is free to use however if you find it useful, and are able to, I’d appreciate a coffee, or if you like you could purchase the plugin.

Image Credit: Shuffle by ProSymbols from the Noun Project.

Getting Started

In order to use the library you will need to include it in your project, to do so please see this guide.


API

Scrappy.Fonts:init( params )

Initiates the library.

params ( number or table ) – Either the seed value as a number, or a table containing a number property called “seed”.


Scrappy.Random:seed( value )

Seeds the random number generator.

value ( number ) – The value to seed the generator with.


Scrappy.Random:inRange( min, max )

Gets a random number within a range.

min ( number ) – The minimum number in the range. Optional, defaults to 0.

max ( number ) – The maximum number in the range. Optional, defaults to the min if not passed in.

returns – The random number.


Scrappy.Random:fromList( table )

Gets a random value from a list.

list ( table ) – The list of values.

returns – The randomly selected value.


Scrappy.Random:string( length, options )

Generates a random string.

length ( number ) – The desired length of the string to generate.

options ( table ) – Table containing options for the generation. Supported options are; ‘includeSymbols’, ‘includeNumbers’, ‘includeLowercase’, ‘includeUppercase’, ‘includeSpaces’, ‘excludeDuplicates’, ‘beginWithALetter’. Optional, leaving it out will include everything.

returns – The randomly generated string.


Scrappy.Random:coinFlip()

Flips a coin.

returns – True or false.


Scrappy.Random:colour()

Gets a random colour.

returns – Three values, the r, g, and b of the colour.


Scrappy.Random:destroy()

Destroys this library.


Examples

Create a random ‘user’.

-- Initiate and seed the generator
Scrappy.Random:init( os.time() )

-- Create some usernames
local usernames =
{
    "alice",
	"bob",
	"scrappyFerret",
	"glitchGames",
	"jason"
}

-- Choose a username from our names table
local username = Scrappy.Random:fromList( usernames )

-- Generate a 20 character long password that begins with a letter but doesn't include spaces
local password = Scrappy.Random:string( 20, { includeSpaces = false, beginWithALetter = true } )

-- Get a colour for the profile
local profileColour = Scrappy.Random:colour( true )

-- Decide their age
local age = Scrappy.Random:inRange( 1, 120 )

-- Flip a coin to see if they should have admin privileges. That's how everyone does it right?
local admin = Scrappy.Random:coinFlip()

-- Display the newly created user
print( "User Created - \nUsername: " .. username .. "\nPassword: " .. password .. "\nProfile Colour: " .. profileColour[ 1 ] .. ", " .. profileColour[ 2 ] .. ", " .. profileColour[ 3 ] .. "\nAge: " .. age .. "\nIs Admin?: " .. ( admin and "Yes" or "No" ) )