You could use randomElement()

'user_id' => $faker->randomElement([1, 3]),

https://github.com/fzaninotto/Faker#fakerproviderbase

Answer from Jasper on Stack Overflow
🌐
FakerPHP
fakerphp.org › formatters › numbers-and-strings
Numbers and Strings - FakerPHP / Faker
By default, an integer is generated between 0 and 2,147,483,647 (32-bit integer). echo $faker->numberBetween(); // 120378987, 182, 102310983 echo $faker->numberBetween(0, 100); // 32, 87, 91, 13, 75
🌐
GitHub
github.com › fzaninotto › Faker › issues › 1512
$faker->unique()->numberBetween(1, 20) · Issue #1512 · fzaninotto/Faker
June 21, 2018 - I'm using Laravel and I have 2 factory states: user and post. I generate 20 users, and 50 posts. Now the interesting part, here are the 2 states, redundent code removed: User: factory(App\Photo::Class, 20)->states('user')->create(); $factory->state(App\Photo::class,'user', function (Faker $faker) { return ([ 'imageable_id' => $faker->unique()->numberBetween(1,20), 'imageable_type' => 'App\User' ]); });
Author   datner
🌐
Shortcode
shortcode.dev › laravel › faker.html
Laravel cheatsheet > Faker
#laravel#db ... $faker = Faker\Factory::create(); $faker->name; // First and second name $faker->randomDigit; // A random number $faker->word; // A single word $faker->sentence; // A sentence $faker->unique()->word; // A single unique word $faker->text($maxNbChars = 300); // 300 character long text $faker->safeEmail; // An email address $faker->hexcolor; // Hex color copy
🌐
GitHub
github.com › fzaninotto › Faker
GitHub - fzaninotto/Faker: Faker is a PHP library that generates fake data for you · GitHub
A faker generator has many of them, packaged in "providers". Here is a list of the bundled formatters in the default locale. randomDigit // 7 randomDigitNot(5) // 0, 1, 2, 3, 4, 6, 7, 8, or 9 randomDigitNotNull // 5 randomNumber($nbDigits = NULL, $strict = false) // 79907610 randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932 numberBetween($min = 1000, $max = 9000) // 8567 randomLetter // 'b' // returns randomly ordered subsequence of a provided array randomElements($array = array ('a','b','c'), $count = 1) // array('c') randomElement($array = array ('a','b','c')) // 'b' shuffle('hello, world') // 'rlo,h eoldlw' shuffle(array(1, 2, 3)) // array(2, 1, 3) numerify('Hello ###') // 'Hello 609' lexify('Hello ???') // 'Hello wgt' bothify('Hello ##??') // 'Hello 42jz' asciify('Hello ***') // 'Hello R6+' regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'); // sm0@y8k96a.ej
Starred by 26.7K users
Forked by 3.6K users
Languages   PHP
Find elsewhere
🌐
Welcm Learning
welcm.uk › blog › getting-started-with-faker-in-laravel
Getting Started With Faker in Laravel | Welcm Learning. Blog
If the output cannot be unique (e.g. if we were to use randomDigitNotNull (this generates a single digit between 1 and 9), to create 10 values, Faker will throw an exception as there are only 9 unique numbers available.
🌐
Laracasts
laracasts.com › discuss › channels › general-discussion › fakers-random-digit-unique
Laracasts
November 3, 2014 - We cannot provide a description for this page right now
🌐
ZetCode
zetcode.com › php › faker
PHP Faker - generating fake data in PHP with Faker package
February 16, 2025 - PHP Faker tutorial shows how to generate fake data in PHP with Faker package.
Top answer
1 of 6
125

Update 2023 for v8

Now moved to https://github.com/faker-js/faker, they changed it again:

faker.number.int(options: number | {
  max: number,
  min: number
} = {}): number
faker.number.int() // 2900970162509863
faker.number.int(100) // 52
faker.number.int({ min: 1000000 }) // 2900970162509863
faker.number.int({ max: 100 }) // 42
faker.number.int({ min: 10, max: 100 }) // 57

source: https://fakerjs.dev/api/number.html#int

Update 2021

Latest versions changed location of the function from faker.random.number to faker.datatype.number, https://github.com/Marak/faker.js/issues/1156

Original answer

You need to give an object to the function:

faker.datatype.number({
    'min': 10,
    'max': 50
});

So if you just pass a number, it will set it as the max value. The min value is 0 by default.

This is the implementation of the number function :

this.number = function (options) {

    if (typeof options === "number") {
      options = {
        max: options
      };
    }

    options = options || {};

    if (typeof options.min === "undefined") {
      options.min = 0;
    }

    if (typeof options.max === "undefined") {
      options.max = 99999;
    }
    if (typeof options.precision === "undefined") {
      options.precision = 1;
    }

    // Make the range inclusive of the max value
    var max = options.max;
    if (max >= 0) {
      max += options.precision;
    }

    var randomNumber = options.precision * Math.floor(
      mersenne.rand(max / options.precision, options.min / options.precision));

    return randomNumber;

  }
2 of 6
42

From Fakerjs github

Whole Number faker. random.number(min,max) Random number between 0 and "range".

faker.random.number(100); //returns 92
faker.random.number({min:5, max:10}); //returns 9

Decimal number faker. finance.amount(min,max,decimal places) Random number between "min" and "max" including decimals to X digits.

faker.finance.amount(9000,10000,4); //returns 9948.8363

Boolean faker. random.boolean()

faker.random.boolean(); //returns true

Array Element faker. random.arrayElement(array[]) Selects a random element from an array of possible values. This function is useful to create custom lists of possibilities.

faker.random.arrayElement(["one","two","three","four"]); //returns "two"
var phTyp = faker.random.arrayElement(["cell","work","home"]); //returns "work"

Object Element faker. random.objectElement(object{}) Selects a random element from an object, selects the value not the keys. This function is useful to create custom lists of possibilities.

faker.random.objectElement({one: 1, two: 2, three: 3}); //returns 3
🌐
Laravel
laravel.com › docs › 7.x › database-testing
Database Testing | Laravel 7.x - The clean stack for Artisans and agents
Within the Closure, which serves as the factory definition, you may return the default test values of all attributes on the model. The Closure will receive an instance of the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing.