Scrappy Random

Scrappy Random is a simple helper library for Solar2D that allows for dealing with random things.

As with all my plugins it is free to use however if you find it useful, and are able to, I’d appreciate a coffee.

Image Credit: Shuffle by ProSymbols from the Noun Project.

Getting Started

There are multiple ways to include the library in your project, the simplest is probably via the plugin, as outlined below, however if you wish you can also access the source code.

Plugin

build.settings


settings =
{
    plugins =
    {
        ["plugin.scrappyRandom"] =
        {
            publisherId = "com.scrappyferret",
            supportedPlatforms =
            {
                iphone = { url="https://plugins.scrappyferret.com/scrappyRandom/iphone.tgz" },
                android = { url="https://plugins.scrappyferret.com/scrappyRandom/android.tgz" },
                macos = false,
                win32 = false
            },
        },
    },
}

main.lua


require( "plugin.scrappyRandom" )

Scrappy.Random:init()

Source Code

If you wish you to access the source code directly it is available on GitLab here.

You can then clone or fork it if you wish, or add the library as a submodule to your project with this command:

git submodule add https://gitlab.com/scrappyferret-libs/scrappy-random.git random

However you get the source code into your project, you will then need to include it and initiate it.

main.lua


require( "random.core" )

Scrappy.Random:init()

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" ) )