How do I generate a random number between a specific range?

Path Finder

Hi, How can I generate a random number between 1 to 20. I random() function doesn't allow to specify a range. please help

Tags (1)


Building a bit on @richgalloway 's answer I believe this works and allows you to set the high and low range. I added just some extra fluff for testing, your eval line could just look line 4 with high and low replaced with your range:

| makeresults
| eval high = 100
| eval low = 10
| eval rand = round(((random() % high)/(high)) * (high - low) + low)
| eval inRange = if(rand <= high AND rand >= low, "True", "False")

Esteemed Legend

@ppablo_splunk , this one should get moderator-answered.

0 Karma


Try using the modulo operator. For example, ... | eval rand=(random() % 20) + 1 | ....

If this reply helps you, an upvote would be appreciated.
.conf21 CFS Extended through 5/20!

Don't miss your chance
to share your Splunk
wisdom in-person or
virtually at .conf21!

Call for Speakers has
been extended through
Thursday, 5/20!