Monday, July 20, 2020

Difference between rand and randc keyword in system verilog

Hi Everyone !
 In this section dealing about rand,randc keywords and usage.

Rand Method:
                  By using this method we we can generate a random number. Their values are uniformly distributed. So the generated number can be repeated.
                   Rand can be used in all constraint.
            Real time example for rand keyword:
                   Think of rolling dice or coin. For every roll there is a equal probability of getting new value of repeated current one. There is a chance of  equal probability  of every occurrence.

Randc Method: 
            Randc keyword are random-cyclic. The basic idea is that randc iterated over all the values in the range and no value is repeated within an iteration. Or in other words solver does not repeat a random value until every possible value generation.Once the all the iteration is finished, a new iteration is automatically generated. For efficient memory usage maximum 8 bit is used. 
            Some constraints not allowed to declare a variable as randc. Usually solver first solve the randc keyword. Randc keyword is not supported in Distribution and solve before constraint 
             Real time example of randc keyword:
                     Think of dealing cards from a deck: you deal out every card in the deck in random order, then shuffle the deck, and deal out the cards in a different order. Note that the cyclic pattern is for a single variable. 

    Example:
            class Practice;
               rand bit[2:0] data1;
               randc bit[2:0] data2;     
            endclass

             module TB;
                  Practice p;
                  initial begin
                   p=new();
                    repeat (5) begin
                           p.randomize();
                            $display(" Data1=%0d Data2=%0d",p.data1,p.data2);
                    end
                    end
                endmodule

Simulator output:
                      Data1=3    Data2=  4
                      Data1=.4   Data2=  3
                      Data1= 2   Data2=  0
                      Data1= 3   Data2=  6 
                      Data1= 1   Data2 = 7
                
Data1 is declared as rand so it may produce a repeated value.(3 value is repeated twice)
Data2 is declares as randc so it won't produce a repeated value until every cycle completion.


TRICKY Interview question:
 How to implement randc functionality without using randc keyword? or Generate a unique numbers without using unique keyword ?. Here I am generating unique random values upto 15. 

                

class unique_value;

  rand int data[];

  constraint c1{foreach (data[i]) data[i] inside {[0:15]};}

  constraint c2{ foreach(data[i])

                {foreach(data[j])

                {if(i!=j)

                data[i]!=data[j];}}}

 

 endclass

 

module test();

  unique_value uq;

  initial begin

    uq = new();

    uq.data=new[15];

    uq.randomize();

    $display("Array = %p",uq.data);

  end

endmodule

 

OUTPUT: -       # Array = '{0, 14, 7, 15, 5, 9, 1, 8, 11, 12, 6, 3, 4, 10, 13}


Hope this code will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM

                                                              "Share your knowledge. It is the way to achieve immortality"
                                                                                                  -Dalai Lama

5 comments:

  1. Good explanation, added to the points above, please describe the rand/randc in class variables. And in module how can we randomize. Without using rand/randc/constraint how can we generate such sequence.

    ReplyDelete
    Replies
    1. Thanks for the Feed Back!!! Will be updated on Next blog. Keep supporting.

      Delete
    2. Feedback is updated in the blog.
      https://vlsicsinfo.blogspot.com/search/label/How%20to%20Randomize%20a%20Non%20Randomize%20Variable

      Delete
  2. Thank-you. I faced this question in Interview. Very helpful.

    ReplyDelete

`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...