System Verilog : Rand & Randc

System Verilog : Rand & Randc

There are two type-modifier keywords available in system Verilog to declare class variable as random.

Variables declared with the rand keyword are standard random variables. Their values are uniformly distributed over their range.

Variables declared with the randc keyword are random-cyclic variables that cycle through all the values in a random permutation of their declared range.

typedef enum {ALEX="ALEX",BOB="BOB",CHI="CHI",DIP="DIP",ETY="EIY",FENU="FENU",GOG="GOG",HIVU="HIVU"} students;

class rand_randc_class;

rand students students_r; randc students students_rc;

endclass

program myprog; rand_randc_class c1 = new;

initial begin

$display("RAND PERM 1=============================================================="); repeat (8) begin c1.randomize(); $display ("Using Rand Student : %s ", c1.students_r); end

$display("RANDC PERM 1 ============================================================"); repeat (8) begin c1.randomize(); $display ("Using RandC Student : %s ", c1.students_rc); end

$display("RAND PERM 2=============================================================="); repeat (8) begin c1.randomize(); $display ("Using Rand Student : %s ", c1.students_r); end

$display("RANDC PERM 2 ============================================================"); repeat (8) begin c1.randomize(); $display ("Using RandC Student : %s ", c1.students_rc); end

$display("RAND PERM 3=============================================================="); repeat (8) begin c1.randomize(); $display ("Using Rand Student : %s ", c1.students_r); end

$display("RANDC PERM 3 ============================================================"); repeat (8) begin c1.randomize(); $display ("Using RandC Student : %s ", c1.students_rc); end

end

endprogram

/* RAND PERM 1============================================================== Using Rand Student : EIY Using Rand Student : ALEX Using Rand Student : FENU Using Rand Student : EIY Using Rand Student : HIVU Using Rand Student : BOB Using Rand Student : ALEX Using Rand Student : DIP RANDC PERM 1 ============================================================ Using RandC Student : EIY Using RandC Student : HIVU Using RandC Student : FENU Using RandC Student : BOB Using RandC Student : GOG Using RandC Student : ALEX Using RandC Student : CHI Using RandC Student : DIP RAND PERM 2============================================================== Using Rand Student : BOB Using Rand Student : GOG Using Rand Student : CHI Using Rand Student : CHI Using Rand Student : EIY Using Rand Student : CHI Using Rand Student : GOG Using Rand Student : BOB RANDC PERM 2 ============================================================ Using RandC Student : BOB Using RandC Student : DIP Using RandC Student : FENU Using RandC Student : GOG Using RandC Student : CHI Using RandC Student : EIY Using RandC Student : ALEX Using RandC Student : HIVU RAND PERM 3============================================================== Using Rand Student : FENU Using Rand Student : BOB Using Rand Student : EIY Using Rand Student : ALEX Using Rand Student : CHI Using Rand Student : EIY Using Rand Student : GOG Using Rand Student : GOG RANDC PERM 3 ============================================================ Using RandC Student : CHI Using RandC Student : GOG Using RandC Student : ALEX Using RandC Student : FENU Using RandC Student : HIVU Using RandC Student : BOB Using RandC Student : DIP Using RandC Student : EIY */

In the above code all names are declared as rand (students_r) and randc (students_rc).

Randomization result for students_r and students_rc are shown above. In this we can see that students_rc (randc) cycle through all the values in a permutation.

class rand_randc_class; rand bit [1:0] r; randc bit [1:0] rc;

endclass

program myprog; rand_randc_class c1 = new;

initial begin

$display("RAND Perm1 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(); $display ("Rand r = %d ",c1.r); end $display("RANDC Perm1 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(; $display ("Randc rc = %d -> ",c1.rc); end

$display("RAND Perm 2------------------"); for(int j =0; j<4; j++) begin c1.randomize(); $display ("Rand r = %d ",c1.r); end $display("RANDC Perm 2 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(; $display ("Randc rc = %d ->",c1.rc); end

$display("RAND Perm3 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(); $display ("Rand r = %d ",c1.r); end $display("RANDC Perm3 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(; $display ("Randc rc = %d -> ",c1.rc); end

$display("RAND Perm 4------------------"); for(int j =0; j<4; j++) begin c1.randomize(); $display ("Rand r = %d ",c1.r); end $display("RANDC Perm 4 ------------------"); for(int j =0; j<4; j++) begin c1.randomize(; $display ("Randc rc = %d ->",c1.rc); end

end

endprogram

/* RAND Perm1 ------------------ Rand r = 0 Rand r = 0 Rand r = 1 Rand r = 0 RANDC Perm1 ------------------ Randc rc = 1 -> Randc rc = 2 -> Randc rc = 3 -> Randc rc = 0 -> RAND Perm 2------------------ Rand r = 2 Rand r = 3 Rand r = 2 Rand r = 1 RANDC Perm 2 ------------------ Randc rc = 3 -> Randc rc = 2 -> Randc rc = 0 -> Randc rc = 1 -> RAND Perm3 ------------------ Rand r = 2 Rand r = 0 Rand r = 2 Rand r = 2 RANDC Perm3 ------------------ Randc rc = 0 -> Randc rc = 3 -> Randc rc = 1 -> Randc rc = 2 -> RAND Perm 4------------------ Rand r = 3 Rand r = 1 Rand r = 0 Rand r = 2 RANDC Perm 4 ------------------ Randc rc = 1 -> Randc rc = 2 -> Randc rc = 0 -> Randc rc = 3 ->

*/

Another example to show that randc cycle through all the values in a permutation.

RANDC Perm1 ------------------ Randc rc = 1 -> Randc rc = 2 -> Randc rc = 3 -> Randc rc = 0 -> RANDC Perm2 ------------------ Randc rc = 3 -> Randc rc = 2 -> Randc rc = 0 -> Randc rc = 1 -> RANDC Perm3 ------------------ Randc rc = 0 -> Randc rc = 3 -> Randc rc = 1 -> Randc rc = 2 -> RANDC Perm4 ------------------ Randc rc = 1 -> Randc rc = 2 -> Randc rc = 0 -> Randc rc = 3 ->