How to use the random function from lodash

Find comprehensive JavaScript lodash.random code examples handpicked from public code repositorys.

lodash.random is a function in the Lodash library that generates a random number within a specified range.

6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
* @param {number} [max=1] The maximum possible value.
* @param {boolean} [floating=false] Specify returning a floating-point number.
* @returns {number} Returns a random number.
* @example
*
* _.random(0, 5);
* // => an integer between 0 and 5
*
* _.random(5);
* // => also an integer between 0 and 5
fork icon73
star icon711
watch icon29

+ 7 other calls in file

309
310
311
312
313
314
315
316
317
318
module.exports.pullAllBy           = _.pullAllBy;
module.exports.pullAllWith         = _.pullAllWith;
module.exports.pullAt              = _.pullAt;
module.exports.quaternary          = _.quaternary;
module.exports.rCurry              = _.rCurry;
module.exports.random              = _.random;
module.exports.range               = _.range;
module.exports.rangeRight          = _.rangeRight;
module.exports.rcurry2             = _.rcurry2;
module.exports.rcurry3             = _.rcurry3;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.random work?

lodash.random is a method in the Lodash library that generates a random number between a given minimum and maximum value, including fractional values, based on the parameters passed to it. The method can accept either one or two arguments, with the first argument representing the upper bound of the random number range, and the second argument (optional) representing the lower bound.

6
7
8
9
10
11
12
13
14
15
16


function getRandChars(numChars) {
  let i = 0
  let word = ''
  while (i < numChars) {
    const index = _.random(61)
    word += alphaNum[index]
    i++
  }
  return word
fork icon1
star icon0
watch icon0

587
588
589
590
591
592
593
594
595
596
597
598
599
600
console.log(clamp); // => -5


const inRange = _.inRange(3, 2, 4);
console.log(inRange); // => true


const random = _.random(0, 5);
console.log(random); // => 4




// Object
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
const _ = require("lodash");

const randomNum = _.random(1, 10);

console.log(randomNum);

In this example, lodash.random is used to generate a random integer between 1 and 10 (inclusive), and the result is stored in the randomNum variable. Finally, the value of randomNum is logged to the console.

1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
  value: _.random(1, 100),
  unit: faker.random.string(),
});

const dimensions = JSON.stringify({
  length: _.random(1, 100),
  width: _.random(1, 100),
  depth: _.random(1, 100),
  unit: faker.random.string(),
});
fork icon0
star icon3
watch icon3

+ 9 other calls in file

44
45
46
47
48
49
50
51
52
53
    const courses = [
        {name: 'Puke Science', desc: 'medical definition of vomitting', subject: 'BIOL', cred: 3},
        {name: 'Drawing Squares', desc: 'geometric proofs and defined orthogonality', subject: 'PHIL', cred: 3},
        {name: 'Hammer Time', desc: 'metalurgical malleability and workshop', subject: 'MENG', cred: 4}
    ]
    const visitor = _.random(100000, 1000000000);
    res.render('index', {tittle: 'Home', courses: courses, visitor: visitor});
    
});

fork icon0
star icon0
watch icon1

+ 5 other calls in file

63
64
65
66
67
68
69
70
71
72
console.log('TEST$_.random');

let val;

// [0, 1]
val = _.random();
console.log(val);

// [0, 999]
val = _.random(10 ** 3 - 1);
fork icon0
star icon0
watch icon0

+ 2 other calls in file

245
246
247
248
249
250
251
252
253
254
  }
}
return {
  reportUser,
  assetsId,
  status: _.random(1, 3),
  dispose: reportUser,
  createTime: `2022-${dayjs().add(1, 'month').month()}-${_.random(1, 30)}`,
  designateTime: `2022-${dayjs().add(1, 'month').month()}-${_.random(1, 30)}`,
  phoneNumber: '13984842424',
fork icon0
star icon0
watch icon1

+ 7 other calls in file

4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
			potentialBestBindings.push(terminal.goodBindings[i]);
		}
	}


	//Ok, pick one of the potentialBestBindings at random!
	var goodBindingIndex = _.random(0, potentialBestBindings.length-1);
	var bindingsToUse = potentialBestBindings[goodBindingIndex];
	return bindingsToUse;
};

fork icon0
star icon0
watch icon3

390
391
392
393
394
395
396
397
398
399
});

it('Stress test: 20 rounds of sendMultiSig', async function() {
  for (let round=0; round < 20; round++) {
    const destinationAccount = accounts[2];
    const amount = _.random(1,9);
    const expireTime = Math.floor((new Date().getTime()) / 1000) + 60; // 60 seconds
    const data = crypto.randomBytes(20).toString('hex');

    const operationHash = helpers.getSha3ForConfirmationTx(nativePrefix, destinationAccount, amount, data, expireTime, sequenceId);
fork icon0
star icon0
watch icon1

+ 14 other calls in file

34
35
36
37
38
39
40
41
42
43
  console.log("cadence sensor simulator is stopped");
  return;
}

this.watchCallbacks.forEach(cbFn => cbFn());
const timeout = random(500, PAUSE_DELAY / 2);
const isLongTimeout = random(0, 100) > 90;
this.lastTimeout = setTimeout(
  this.loopFn.bind(this),
  isLongTimeout ? 10000 : timeout,
fork icon0
star icon0
watch icon1

152
153
154
155
156
157
158
159
160
161
		mongo: this._prepareMongo()
	});
}

queryMongo() {
	// let offset = _.random(100, 10000);
	let grp = _.random(0, 2);
	return Post.find({
			group_id: grp
		})
fork icon0
star icon0
watch icon0

+ 3 other calls in file

316
317
318
319
320
321
322
323
324
325
326
327
};


helpers._parseAmount = function(str) {
  var result = {
    amount: +0,
    confirmations: _.random(6, 100),
  };


  if (_.isNumber(str)) str = str.toString();

fork icon0
star icon0
watch icon0

+ 3 other calls in file

929
930
931
932
933
934
935
936
937
938
939
 * @param {String} url
 * @param {Callback} cb
 */
API.prototype._doGetRequest = function(url, cb) {
  url += url.indexOf('?') > 0 ? '&' : '?';
  url += 'r=' + _.random(10000, 99999);
  return this._doRequest('get', url, {}, false, cb);
};


API.prototype._doGetRequestWithLogin = function(url, cb) {
fork icon0
star icon0
watch icon0

9
10
11
12
13
14
15
16
17
18
 * @param {KaiLogs.Logger} logger 
 * @param {Discord.Interaction} interaction 
 */
module.exports = function(logger, interaction) {
    var dice = _.sortBy(_.times(5, function() {
        return _.random(1,6);
    }, function(a, b) {
        return a - b;
    }));

fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)