# numbers

ScalaCheck custom generator examples alvin February 8, 2019 - 5:48pm

Table of Contents1 - Custom generators2 - Built-in ScalaCheck generators3 - How to use ScalaCheck generators4 - More ScalaCheck generators

Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.

## Custom generators

This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:

This is a page from my book, Functional Programming, Simplified

## More sales tips

The most important thing I can tell you about selling is to put down this book, and go buy a copy of How I Raised Myself From Failure to Success in Selling, by Frank Bettger. It’s an old book, but it’s the single most important book on “selling” I have ever read.

In addition to that suggestion, here are a few other important sales tips I know.

## How to compare floating-point numbers in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.5, “Comparing Floating-Point Numbers in Scala.”

## Problem

You need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be.

## How to create a Range, List, or Array of numbers in Scala

Scala FAQ: How can I create a range, list, or array of numbers in Scala, such as in a `for` loop, or for testing purposes?

## Solution

Use the `to` method of the `Int` class to create a `Range` with the desired elements:

```scala> val r = 1 to 10
r: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)```

You can also set the step with the `by` method: