# How can I use $urandom/$random with range?

**URL:** <https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657>\
**Category:** SystemVerilog\
**Tags:** urandom, SystemVerilog\
**Created:** [December 15, 2016, 3:59pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657 "2016-12-15T15:59:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [December 15, 2016, 3:59pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/1 "2016-12-15T15:59:48Z")

</div>

Is there a way to use $urandom/$random with range?

I succeeded to do it with max range in the following way (in this case 11 is the max range):

```verilog
temp = (($urandom(9)%11));

```

But how can I do it for max and min, for example max=11, min =8.

\*I can’t use $urandom\_range because I have to use different seed from test to test.

---

<div class="post-metadata">

**Author:** ![sbellock](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sbellock](https://verificationacademy.com/forums/u/sbellock)\
**Post date:** [December 15, 2016, 4:22pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/2 "2016-12-15T16:22:40Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657):_

The modulus (%) operator gives you the range, so you just increase the range by the minimum value.

```verilog

temp = ($urandom % (max - min + 1)) + min;

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 15, 2016, 9:23pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/3 "2016-12-15T21:23:34Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/2):_  
You should be able to use both $urandom or $urandom\_range without specifying a seed and let SystemVerilog manage seed generation. You can change the global seeding from test to test by using a command line option. In Questa the option is **-sv\_seed \<_arg_\>** where \<_arg_\> is **random** or a specific number.

---

<div class="post-metadata">

**Author:** ![saritr](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/df705f/32.png) [@saritr](https://verificationacademy.com/forums/u/saritr)\
**Post date:** [December 18, 2016, 9:11am UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/4 "2016-12-18T09:11:47Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/3):_

I have to use seed because needs of repreduce bugs.  
I use ModelSim, not Questa.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [December 18, 2016, 4:10pm UTC](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/5 "2016-12-18T16:10:23Z")

</div>

_In reply to [saritr](https://verificationacademy.com/forums/t/how-can-i-use-urandom-random-with-range/32657/4):_  
You might want to check if Modelsim also supports the -sv\_seed option.
