Random

<script src="./point_src/random"></script>

The random class provides a range of methods for producing random values.

The random object is instansiated for free:

const random = new Random()
// examples
random.int()
random.point()
Meta Data
title Random
dependencies ()
unused_keys ()
unknown_keys ()
filepath_exists True
path random
filepath random.js
clean_files ()

  • ClassDeclaration
    class comments:

    The minimum value the point(value) can be, before the automatic 'float' method is used over the 'int' method.

    • property

      pointIntMin

      = 2
      from Random
      dict_keys(['kind', 'word', 'computed', 'static', 'value', 'type', 'pos', 'class_name'])
    • method

      int

      (
      min = 1 , max
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Generate an integer between 0 and the given min. Given 1 (default), the result will be either 0 or 1

      random.int(10) // 7
      random.int(50) // 30
      random.int(300) // 220
      
    • method

      float

      (
      min = 1 , max
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Generate a decimal number between 0 and 1. Provide a min/max to limit

      random.float()
      random.float(.2, 1)
      
    • method

      callOne

      (
      functions
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Given many functions, call a random one.

    • method

      choice

      (
      selections
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Select and return a random item from an array.

      Use this method to randomly choose one element from a collection of items.

      const colors = ['red', 'blue', 'green', 'yellow']
      random.choice(colors)
      // 'green'
      
      const points = [new Point(10, 20), new Point(30, 40), new Point(50, 60)]
      random.choice(points)
      // Point { x: 30, y: 40 }
      
    • method

      index

      (
      selections
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Return a random index for the given array.

      Use this to get a valid random position within an array's bounds.

      const items = ['apple', 'banana', 'cherry', 'date']
      random.index(items)
      // 2
      
      items[random.index(items)]
      // 'banana'
      
    • method

      string

      (
      multiplier = 1 , rot = 32
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Return a random string

    • method

      radix

      (
      v , rot = 32
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
    • method

      point

      (
      multiplier = 1 , method = undefined
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Generate a new Point with random x and y coordinates.

      Use this to create a random Point instance with coordinates scaled by the multiplier. Values ≤2 use floats, larger values use integers by default.

      random.point(100)
      // Point { x: 47, y: 83 }
      
      random.point(1)
      // Point { x: 0.234, y: 0.891 }
      
      random.point(500, 'float')
      // Point { x: 234.56, y: 412.89 }
      
    • method

      shuffle

      (
      points , max = 1
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Randomly shuffle all points be a random amount between 0 and a multilper of the radius. For example if the point is 20px, a max of .5 would plot the center of the point anywhere within the full point area. Providing a value of 1 (default) would plot within a 40px width and height (circular) area. (Math.PI * 2)

      This is because the max value computes a percenile of the radius of the origin position, resolving anywhere within the diameter area if the point.

    • method

      within

      (
      point , max = 0.5
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Return a random 2D position within the given point.

      within({ x: 100, y: 100, radius: 100})

    • method

      xy

      (
      multiplier = 1
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      return two random values between 0 and 1 multiplied by the given value multiplier. This is similar to the random.point() function, except it only returns an object of {x,y}

    • method

      gaussianFloat

      (
      mean , stdev = 1
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Standard Normal variate using Box-Muller transform.

    • method

      gaussian

      (
      start , end , mean , stdev = 1
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])
    • method

      polar2D

      (
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Generate random 2D coordinates using polar distribution.

      Use this to create randomly distributed points following a normal distribution in both x and y dimensions, useful for particle effects and natural scatter patterns.

      random.polar2D()
      // [0.8234, -1.2156]
      
      const [x, y] = random.polar2D()
      const point = new Point(centerX + x * radius, centerY + y * radius)
      
    • method

      color

      (
      h = 360 , s = 100 , l = 100
      )
      from Random
      dict_keys(['kind', 'word', 'static', 'computed', 'is_symbol', 'value', 'type', 'comments', 'pos', 'class_name'])

      Return a random hsl color string:

      random.color()
      random.color(360, 100, 100)
      random.color(360, [30, 100], [60,100])
      
    dict_keys(['kind', 'word', 'parentName', 'type', 'body', 'comments', 'pos'])
  • VariableDeclaration
    const

    random

    =
    new Random ( )
  • ExpressionStatement

    :

    dict_keys(['type', 'expression', 'pos'])
  • VariableDeclaration
    const

    randomizePoint

    =
    function ( px , y )

    Perform random() on the X and Y.

    • If a point is given, randomize to the max of the given point
    • If a single number is given, assume square
    • If no params are given, discover the stage size.

    Use Point.random() for the same form, as a new point

    let p = new Point()
    p.randomize(100, 400)
    p.randomize(100) // 100, 100
    p.randomize(new Point(100, 400)) // 100, 400
    

    if one of the keys is undefined, no change occurs:

    p.setXY(500,700)
    p.randomize(100, undefined) // 100, 700
    p.randomize(undefined, 400) // 500, 400
    
    p.randomize(undefined, undefined) // 500, 700 // no change occurs.
    

    Note; no params will randomize as much as possible (the stage size)

    p.randomize() // 800, 600
    

    Other features would be nice:

    • random in rect (like stage):

      p.randomize(rect|dimensions)

    • Randomize other values, e.g radius, colors

      p.randomize(['x', 'y', 'radius', 'mass'])

      // randomize x to max 400, radius to max 50 p.randomize({ x: 400, radius: 50})

    • In the future, the origin; randomize relative to a point:

      p.randomize({point, relative: true})