0 votes
in Verilog by (220 points)

How do I create a random in Verilog?

2 Answers

0 votes
by (200 points)

Verilog HDL has a simple set of pseudorandom generator functions: $random[(seed)], $srandom[(seed)], $urandom[(seed)], $random_range().

$srandom - to generate signed numbers,
$urandom - unsigned..
$random_range() is to generate numbers in given range. Also for ranging %: ($random % border) - the result must be in the range of [(-border+1):(border+1)]

`timescale 1ns / 1ps

module tb();

reg clk = 1'b0;
always #10 clk = ~clk;

reg signed [7:0] a = -128, b = -128;
wire signed [8:0] c;

always @(posedge clk) begin
a <= $random % 128;
b <= $random % 128;
$strobe("A = %d, B = %d, A - B = %d", a, b, c);

subtractor dut (a, b, c);

0 votes
by (220 points)

The command $random generates random number in verilog. It returns a 32 bit signed integer. To restrict the number in a particular range use the command as follows:
$random % b
This command will generte a random number in the range [(-b+1):(b-1)]
For example: $random % 2 generates a random number in the range [-1,1]

Hardware Coder Community

© 2022 by Hardware Coder. User contributions are licensed under cc by-sa 4.0 with attribution required. Attribution means a link to the question, answer, user, etc on this site.

This site is owned and operated by Hardware Coder in McKinney, Texas.

Send Us A Message
About Us

By using this site, you agree to the following:

Privacy Policy
Terms and Conditions
DMCA Policy
Earnings Disclaimer
Legal Disclaimer