1.
Generate the pattern 0101010101
Solution (Conditional if-else)
class pattern_gen;
rand bit a[];
constraint pattern_c {
a.size == 10;
foreach (a[i])
if (i % 2 == 0)
a[i] == 0;
else
a[i] == 1;
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Direct Mathematical Assignment)
constraint pattern_alt_c {
a.size == 10;
foreach (a[i])
// The result of (i % 2) directly creates the 0, 1, 0, 1 pattern
a[i] == (i % 2);
}
www.linkedin.com/in/yaswanth-panthangi
2. Generate the pattern 1234554321
Solution (Piecewise Formula)
class pattern_gen;
rand int a[];
constraint pattern_c {
a.size == 10;
foreach (a[i])
if (i < 5) // First half of the array (indices 0-4)
a[i] == i + 1;
else // Second half of the array (indices 5-9)
a[i] == 10 - i;
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Symmetrical Relational Constraint)
constraint pattern_alt_c {
a.size == 10;
foreach (a[i])
if (i < 5)
a[i] == i + 1;
else
// Constrain the second half to be a mirror of the first
a[i] == a[a.size() - 1 - i]; // e.g., a[5]==a[4], a[6]==a[3]
}
www.linkedin.com/in/yaswanth-panthangi
3. Generate the pattern 9 19 29 39 49 59 69 79
Solution (Direct Mathematical Formula)
class pattern_gen;
rand int a[];
constraint pattern_c {
a.size == 8;
foreach (a[i])
a[i] == (i * 10) + 9;
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Relational Constraint)
constraint pattern_alt_c {
a.size == 8;
a[0] == 9; // Anchor the first value
foreach (a[i])
if (i > 0)
a[i] == a[i-1] + 10; // Each subsequent value is 10 more than the
last
}
www.linkedin.com/in/yaswanth-panthangi
4. Generate the pattern 5 -10 15 -20 25 -30
Solution (Conditional if-else)
class pattern_gen;
rand int a[];
constraint pattern_c {
a.size == 6;
foreach (a[i])
if (i % 2 == 0) // Even indices for positive terms
a[i] == 5 * (i + 1);
else // Odd indices for negative terms
a[i] == -5 * (i + 1);
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Mathematical Sign Alternation)
constraint pattern_alt_c {
a.size == 6;
foreach (a[i])
a[i] == ((i % 2 == 0) ? 1 : -1) * (5 * (i + 1));
}
www.linkedin.com/in/yaswanth-panthangi
5. Generate the pattern 1122334455
Solution (Integer Division)
class pattern_gen;
rand int a[];
constraint pattern_c {
a.size == 10;
foreach (a[i])
a[i] == (i / 2) + 1;
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Relational Constraint)
constraint pattern_alt_c {
a.size == 10;
foreach (a[i])
if (i % 2 == 0) // Even index: start a new number
if (i == 0) a[i] == 1;
else a[i] == a[i-1] + 1;
else // Odd index: copy the previous number
a[i] == a[i-1];
}
www.linkedin.com/in/yaswanth-panthangi
6. Generate a random number between 1.35 and 2.57
Solution (Integer Scaling)
class real_gen;
rand int scaled_val;
real final_val;
constraint scaled_c {
// Randomize an integer in the scaled range [1350:2570]
scaled_val inside {[1350:2570]};
}
function void post_randomize();
// After randomization, scale it down to a real number
final_val = scaled_val / 1000.0;
$display("Generated Real Number: %f", final_val);
endfunction
endclass
module test;
real_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Procedural Generation)
module top;
initial begin
real my_real;
// Get a random integer in the range and scale it
my_real = $urandom_range(2570, 1350) / 1000.0;
$display("Random real number: %f", my_real);
end
endmodule
www.linkedin.com/in/yaswanth-panthangi
7. Generate the pattern 0102030405
Solution (Conditional if-else)
class pattern_gen;
rand int a[];
constraint pattern_c {
a.size == 10;
foreach (a[i])
if (i % 2 == 0)
a[i] == 0;
else
a[i] == (i / 2) + 1;
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Single Mathematical Formula)
constraint pattern_alt_c {
a.size == 10;
foreach (a[i])
// (i % 2) acts as a switch: it's 0 for even i, 1 for odd i
a[i] == (i % 2) * ((i / 2) + 1);
}
www.linkedin.com/in/yaswanth-panthangi
8. Generate values from {25, 27, 30, 36, 40, 45} without inside
Solution (Logical Deduction)
class pattern_gen;
rand int a;
constraint c1 {
a > 24;
a < 46;
((a % 5) == 0) || ((a % 9) == 0);
a != 35;
}
function void post_randomize();
$display("Generated Value: %0d", a);
endfunction
endclass
module test;
pattern_gen h1;
initial begin
h1 = new();
repeat (10) begin
void'(h1.randomize());
end
end
endmodule
Alternative Solution (Contradiction Constraint)
constraint value_alt_c {
val inside {[25:45]};
// If a value is NOT a multiple of 5 AND NOT a multiple of 9,
// then create an impossible condition (1 == 0).
(val % 5 != 0 && val % 9 != 0) -> 1 == 0;
val != 35;
}
www.linkedin.com/in/yaswanth-panthangi
9. Generate a random even number between 50 and 100
Solution (Standard Modulo Constraint)
class even_gen;
rand int even_num;
constraint even_c {
even_num inside {[50:100]};
even_num % 2 == 0; // The remainder when divided by 2 must be 0
}
function void post_randomize();
$display("Generated Even Number: %0d", even_num);
endfunction
endclass
module test;
even_gen h1;
initial begin
h1 = new();
repeat (5) void'(h1.randomize());
end
endmodule
Alternative Solution (Solver-Efficient Method)
class even_gen_alt;
rand int half_num; // Randomize a helper variable
int even_num;
constraint half_c {
// The range for the half-number is [50/2 : 100/2]
half_num inside {[25:50]};
}
function void post_randomize();
// Construct the final even number after randomization
even_num = half_num * 2;
$display("Generated Even Number (Alt): %0d", even_num);
endfunction
endclass
www.linkedin.com/in/yaswanth-panthangi
10. 32-bit variable with 12 non-consecutive 1's
Main Solution (Combined Constraints)
class non_consecutive_gen;
rand bit [31:0] data;
constraint valid_data_c {
// Constraint 1: Ensure there are exactly 12 ones
$countones(data) == 12;
// Constraint 2: Ensure no two ones are consecutive
foreach (data[i])
if (i > 0)
// If a bit is 1, the previous bit must be 0
(data[i] == 1) -> (data[i-1] == 0);
}
function void post_randomize();
$display("Generated Data: %b", data);
$display("Number of ones: %0d", $countones(data));
endfunction
endclass
module test;
non_consecutive_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution (Forbidding the "11" Pattern)
constraint valid_data_alt_c {
$countones(data) == 12;
foreach (data[i])
if (i > 0)
// It is illegal for a bit and its predecessor to both be 1
!(data[i] == 1 && data[i-1] == 1);
}
www.linkedin.com/in/yaswanth-panthangi
11. Write a constraint to generate the factorial of the first 5 even and
odd numbers
Solution
class factorial_gen;
rand int even_fact[];
rand int odd_fact[];
// Helper function to calculate factorial
function int fact(int n);
if (n <= 1) return 1;
else return n * fact(n - 1);
endfunction
constraint factorial_c {
even_fact.size == 5;
odd_fact.size == 5;
// Constrain the array for even numbers (2!, 4!, 6!, 8!, 10!)
foreach (even_fact[i])
even_fact[i] == fact(2 * (i + 1));
// Constrain the array for odd numbers (1!, 3!, 5!, 7!, 9!)
foreach (odd_fact[i])
odd_fact[i] == fact((2 * i) + 1); }
function void post_randomize();
$display("Factorials of first 5 even numbers: %p", even_fact);
$display("Factorials of first 5 odd numbers: %p", odd_fact);
endfunction
endclass
module test;
factorial_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
class factorial_gen_alt;
rand int even_fact[];
rand int odd_fact[];
www.linkedin.com/in/yaswanth-panthangi
constraint size_c {
even_fact.size == 5;
odd_fact.size == 5;
}
function int fact(int n);
return (n <= 1) ? 1 : n * fact(n - 1);
endfunction
function void post_randomize();
foreach (even_fact[i])
even_fact[i] = fact(2 * (i + 1));
foreach (odd_fact[i])
odd_fact[i] = fact((2 * i) + 1);
$display("Factorials of first 5 even (alt): %p", even_fact);
$display("Factorials of first 5 odd (alt): %p", odd_fact);
endfunction
endclass
www.linkedin.com/in/yaswanth-panthangi
12. Write a constraint such that even locations contain odd numbers and
odd locations contain even numbers
Solution
class eo_gen;
rand int a[];
constraint even_odd_c {
a.size == 10;
foreach (a[i]) {
a[i] inside {[1:100]};
if (i % 2 == 0) // Even index
a[i] % 2 == 1; // Must contain an odd number
else // Odd index
a[i] % 2 == 0; // Must contain an even number
}
}
function void post_randomize();
$display("Generated Pattern: %p", a);
endfunction
endclass
module test;
eo_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
constraint even_odd_alt_c {
a.size == 10;
foreach (a[i]) {
a[i] inside {[1:100]};
(i % 2 == 0) -> (a[i] % 2 != 0);
(i % 2 != 0) -> (a[i] % 2 == 0);
}
}
www.linkedin.com/in/yaswanth-panthangi
13. Write a program to randomize a 32-bit variable but only randomize the
12th bit
Executable Main Solution
class partial_rand_gen;
rand bit [31:0] data;
constraint randomize_12th_bit_c {
// Constrain every bit EXCEPT bit 11 (the 12th bit) to be 0.
foreach (data[i])
if (i != 11)
data[i] == 0;
}
function void post_randomize();
$display("Randomized data: %b", data);
endfunction
endclass
module test;
partial_rand_gen h1;
initial begin
h1 = new();
repeat (5) void'(h1.randomize());
end
endmodule
Alternative Solution
class partial_rand_gen_alt;
rand bit [31:0] data;
function void pre_randomize();
this.data.rand_mode(0); // Turn rand_mode OFF for all bits
this.data[11].rand_mode(1); // Turn rand_mode ON for only the 12th bit
endfunction
function void post_randomize();
$display("Randomized data (alt): %b", data);
endfunction
endclass
www.linkedin.com/in/yaswanth-panthangi
14. Write a constraint on a two-dimensional array to generate even
numbers in the first 4 locations and odd numbers in the next 4
locations
Executable Main Solution
class array_gen;
rand int a[2][4];
constraint array_eo_c {
foreach (a[i, j]) {
a[i][j] inside {[1:100]};
if (i == 0) // First row (first 4 locations)
a[i][j] % 2 == 0; // Must be even
else // Second row (next 4 locations)
a[i][j] % 2 != 0; // Must be odd
} }
function void post_randomize();
$display("Generated 2D Array: %p", a);
endfunction
endclass
module test;
array_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
constraint array_eo_alt_c {
// Constrain all elements in the first row to be even
foreach (a[0][j]) {
a[0][j] inside {[1:100]};
a[0][j] % 2 == 0;
}
// Constrain all elements in the second row to be odd
foreach (a[1][j]) {
a[1][j] inside {[1:100]};
a[1][j] % 2 != 0;
}}
www.linkedin.com/in/yaswanth-panthangi
15. Generate an array with unique values and multiples of 3
class unique_mult3_gen;
rand int a[];
constraint unique_mult3_c {
a.size == 10;
unique {a};
foreach (a[i]) {
a[i] inside {[1:100]};
a[i] % 3 == 0;
} }
function void post_randomize();
$display("Generated Array: %p", a);
endfunction
endclass
module test;
unique_mult3_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
class unique_mult3_gen_alt;
rand int unique_helpers[];
int a[10];
constraint helpers_c {
unique_helpers.size == 10;
unique {unique_helpers};
foreach (unique_helpers[i])
unique_helpers[i] inside {[1:33]}; }
function void post_randomize();
foreach (a[i])
a[i] = unique_helpers[i] * 3;
$display("Generated Array (alt): %p", a);
endfunction
endclass
www.linkedin.com/in/yaswanth-panthangi
16. Write a constraint to generate unique numbers in an array without using
the "unique" keyword
class unique_gen;
rand int a[];
constraint unique_c {
a.size == 10;
foreach (a[i])
a[i] inside {[1:100]};
foreach (a[i, j])
if (i != j)
a[i] != a[j]; }
function void post_randomize();
$display("Generated Unique Array: %p", a);
endfunction
endclass
module test;
unique_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
module unique_alt;
int a[10];
initial begin
// 1. Create an array with unique, sequential values
foreach (a[i])
a[i] = i + 1;
$display("Array before shuffle: %p", a);
// 2. Shuffle the array to randomize the order
a.shuffle();
$display("Array after shuffle: %p", a);
end
endmodule
www.linkedin.com/in/yaswanth-panthangi
17. Write a constraint to generate a variable with 0-31 bits as 1 and 32-61
bits as 0
Solution
class bit_pattern_gen;
rand bit [61:0] data;
constraint pattern_c {
foreach (data[i])
if (i <= 31)
data[i] == 1;
else
data[i] == 0;
}
function void post_randomize();
$display("Generated Data: %b", data);
endfunction
endclass
module test;
bit_pattern_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
constraint pattern_alt_c {
data[31:0] == '1; // Constrain the lower 32 bits to all ones
data[61:32] == '0; // Constrain the upper 30 bits to all zeros
}
www.linkedin.com/in/yaswanth-panthangi
18. Write a constraint to generate consecutive and non-consecutive
elements in a fixed-size array
Solution (Consecutive)
class consecutive_gen;
rand int a[10];
constraint consecutive_c {
foreach (a[i])
a[i] inside {[0:100]};
foreach (a[i])
if (i > 0)
a[i] == a[i-1] + 1; }
function void post_randomize();
$display("Consecutive Array: %p", a);
endfunction
endclass
module test_consecutive;
consecutive_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Solution (Non-Consecutive)
class non_consecutive_gen;
rand int a[10];
constraint non_consecutive_c {
unique {a};
foreach (a[i]) a[i] inside {[1:100]}; }
Endclass
module test_non_consecutive;
non_consecutive_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
$display("Non-Consecutive (Unique) Array: %p", a);
end
endmodule
www.linkedin.com/in/yaswanth-panthangi
20. Write a constraint to randomly generate 10 unique numbers between 99
and 100
Solution
class real_gen;
rand int scaled_vals[];
real final_vals[];
constraint unique_real_c {
scaled_vals.size == 10;
final_vals.size == 10;
unique {scaled_vals};
// Randomize integers allowing for precision, e.g., [99000:100000] for
3 places
foreach (scaled_vals[i])
scaled_vals[i] inside {[99000:100000]};
}
function void post_randomize();
foreach (scaled_vals[i])
final_vals[i] = scaled_vals[i] / 1000.0;
$display("Generated Unique Real Numbers: %p", final_vals);
endfunction
endclass
module test;
real_gen h1;
initial begin
h1 = new();
void'(h1.randomize());
end
endmodule
Alternative Solution
module unique_real_alt;
real results[$];
real temp_real;
initial begin
www.linkedin.com/in/yaswanth-panthangi
while (results.size() < 10) begin
temp_real = $urandom_range(100000, 99000) / 1000.0;
if (results.find(x) with (x == temp_real).size() == 0)
results.push_back(temp_real);
end
$display("Generated Unique Reals (alt): %p", results);
end
endmodule
www.linkedin.com/in/yaswanth-panthangi