Code Monkey home page Code Monkey logo

random_in's Introduction

random_in

A generator of random numbers. Implemented as a built-in Integer class' method.

Generates an array of specified quantity of random numbers. Generated numbers may repeat or may not repeat - it is configurable. You can set a range of numbers from which to generate by defining the beggining and the end of the range. Ruby's Random class is used as generator. Tested.

How it works

# Generated numbers are all unique
rs = 5.randoms_in(from = 1, to = 10, may_repeat = false) #=> [4, 8, 9, 10, 1]

# Generate numbers may repeat
rs = 5.randoms_in(1, 10 , true) #=> [7, 2, 3, 2, 6]

Signature: int.randoms_in(int|string, int|string, bool)

What's special

Two forms of the name - plural and singular:

randoms_in and random_in

# Singular
1.random_in(1, 10, false) #=> [3]

# Plural
5.randoms_in(1, 10, false) #=> [4, 5, 9, 2, 7]

However, no errors will be thrown if switch or confuse them. This is just a syntactic sugar.

Exceptions thrown

The plugin may throw two its own exceptions. Both of them are ArgumentError exeptions:

  1. The result set cannot be less than your number when parameter may_repeat is false:
# error
3.randoms_in(from = 1, to = 2, may_repeat = false) # rs.length == 2, but you requested 3
3.randoms_in(-1, 0, false) # the same, rs.length == 2, but you requested 3

# no error
3.randoms_in(1, 2, true) # doesn't matter what is your number, numbers may repeat in rs
3.randoms_in(-1, 0, true)
  1. Parameter from has to be less than parameter to:
5.randoms_in(from = 10, to = 1, may_repeat = false) <-- error
5.randoms_in(1, 10, false) <-- no error

How to get

random_in's People

Contributors

wzup avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.