Overview

In Verilog, randomization refers to the process of generating random or pseudo-random values for variables or signals during simulation. Randomization is used to create various test scenarios, stimulate different input conditions, and uncover corner cases in a design's behavior. It's a crucial technique in functional verification to ensure that a design functions correctly across a wide range of possible inputs.

$random
$urandom
$random_range( min, max )

Theory Questions
Coding Questions
    module top;
    
        reg [31:0] RAND;
    
        initial
        begin
            RAND = $urandom;
            RAND[0] = 1;
            
            $display("RAND : %0d", RAND);
        end
    
    endmodule
    
    function getNum;
        return $urandom%100;    
    endfunction
    
    module top;
    
        reg [31:0] RAND;
    
        initial
        begin
            RAND = getNum();
            
            $display("RAND : %0d", RAND);
        end
    
    endmodule
    
    function getMaxMinRange( integer max, integer min );
        return ($urandom%(max-min))+min+1;    
    endfunction
    
    module top;
    
        reg [31:0] RAND;
    
        initial
        begin
            RAND = getMaxMinRange(30,5);
            
            $display("RAND : %0d", RAND);
        end
    
    endmodule
    
    function getRand;
        return ($urandom/3)*3;
    endfunction
    
    module top;
    
        reg [31:0] RAND;
    
        initial
        begin
            RAND = getRand();
            
            $display("RAND : %0d", RAND);
        end
    
    endmodule
    
    module unique_digit_random;
    
      function integer dec_rand_num();
        integer number;
        integer i, digit, count;
        reg [3:0] used [0:9];      // To track if a digit is used
    
        i = 0;
        digit = 0;
    
        number = 0;
        count = 0;
    
        // Reset the used digits array
        for (i = 0; i < 10; i = i + 1)
          used[i] = 0;
    
        // Generate a random number with unique digits
        while (count < 6) begin // 6 digits
          digit = $urandom % 10; // Random digit between 0 and 9
          if (!used[digit]) begin
            used[digit] = 1;                // Mark digit as used
            number = number * 10 + digit;  // Append digit to the number
            count = count + 1;
          end
        end
    
        return number;
      endfunction
    
      initial begin
        repeat(10) begin
          $display("Number : %0d", dec_rand_num());
        end
      end
    endmodule
    
    Locked Solution
    function integer getWeight();
        return {$urandom,(($urandom%100)<20)?1'b0:1'b1};
    endfunction
    
    module top;
    
        integer RAND;
        integer i;
    
        initial
        begin
            for( i = 0; i < 20; i=i+1 )
            begin
                RAND = getWeight();
                $display("RAND : %0d", RAND);
            end
        end
    
    endmodule
    
    function reg [31:0] getUs();
        return {$urandom>>1};
    endfunction
    
    module top;
    
        integer RAND;
    
        initial
        begin
            RAND = getUs();
            $display("RAND : %0d", RAND);
        end
    
    endmodule
    
    Locked Solution
    function reg [63:0] get64();
        return {$urandom,$urandom};
    endfunction
    
    module top;
    
        reg [63:0] RAND;
    
        initial
        begin
            RAND = getUs();
            $display("RAND : %0d", RAND);
        end
    
    endmodule