Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I can easily generate a random number of width 32 bits in Verilog using $random. Is there a way to generate a random number of exactly n bits (say n = 70)?

I guess I could concatenate many 32-bits random numbers, and then restrict down to the required number of bits, but that seems like a hack.

share|improve this question
1  
Seems like a reasonable hack to me, given the Verilog simulator will need to actually do its calculations using the underlying machine architecture, which probably deals with 32 or 64 bit words rather than single bits at a time. – The Photon Sep 3 '12 at 15:33

1 Answer

up vote 3 down vote accepted

If you are able to use SystemVerilog, you can randomize a number of any width. Either declare it as rand within a class, or use std::randomize. Here is a simple example:

module top;

  bit[69:0] vec;

  initial begin
    assert(std::randomize(vec));
    $display("vec = %070b", vec);
  end

endmodule

If you need to stick to plain old Verilog, I think the hack you suggested is the best, simplest, and possibly the only choice. I don't think there is anything in the language to help, since $random returns a 32-bit number.

If you simply want to avoid declaring new variables and explicitly concatenating them, you could do something like this.

vec[31:0]  = $random;
vec[69:32] = $random;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.