Constraint for a random variables

There are rand variables a and b each 8-bits wide. Writer a constraint such that 10 percent of the time they should be equal. They can have any values rest of the time.

We tried by the following method(eda link below). But variable b is randomized to single bit.

This works.

class random2;
rand bit[7:0]a,b;

//constraint a_con{a==b;}
  constraint b_con{(a==b) dist {1:=10,0:=90};
}

function void display();
$display("a=%d,b=%d",a,b);
endfunction

endclass

module ex;
random2 a_h;
initial begin
a_h=new();
repeat(200)
begin
a_h.randomize();
a_h.display();
end
end
endmodule

In reply to n347:

It’s working. Thanks a lot.

In reply to n347:

Nothing to add other than thanks for both the example and solution. Reading the original question, I thought “Homework / Interview Question”. Which is ok - I like puzzles - I brainstormed various solutions.

But the super compact solution @n347 posted, was just… really neat. I still have a lot to learn about the strength and abilities of the SystemVerilog constraint solver.

From reading these forums, I know there’s certainly potholes to watch out for. But this is a powerful tool.

In reply to n347:

Hi When you run this for 100 times it shows out of 100, 13 times they are same. so that means we got it both same for 13% and not 10%.when we increase number of times it runs, it goes near to 10% like for 400 times it gives 10.75%. can you please check.

Yes for smaller runs it will be around 13%, for longer runs only you get around 10%. While using weighted distribution all the program will have similar output. You have to increase number of runs to get the required percentage.

In reply to Chandra Shekar N:

By saying this, weighted distribution don’t provide probability of values, They are merely just weights of numbers comes up as solutions, Is there any other solution where probability of numbers is not dependent on number of times you randomize object.

In reply to juhi_p:

You need to go back to your probability and statistics courses. Let’s take the classic example of flipping a coin what are you have a 50-50 chance of heads or tails. If you flip that coin twice there are 4 possible outcomes but only 2 of them result in one head and one tail. And 1 possible outcome of all heads. We can transfer the concept of flipping a coin to a call to randomize of a single bit. Then we can say if you call randomize two times, you only have a 50% probability of seeing exactly one 1 and one 0.

If we double the calls to randomize() to 4 times, now the number of outcomes with exactly two 1’s drops to 6 out of 16, or 37.5%. But the probability of seeing all 1’s as an outcome drops from 25% to 6.25%. There is also a 25% chance of seeing one 1 and onother 25% chance of seeing three 1’s.

If we keep increasing the number of calls to randomize, you’ll see that the probability of getting exactly 50-50 1s and 0s keeps going down, but the probability of getting an outcome closer to 50-50 keeps increasing. This is a property of a normal distribution curve.

Agreed to everyone,

Just to add, if you want more close to probability over short runs- try this


class pack;
 
  rand logic [7:0] a,b;
  randc int p;
  
  constraint p_c{
    p inside {[1:100]};
    p<=40 -> a==b;
    p>40 -> a!=b;
  }
  
  function new();
    this.randomize();
  endfunction
  function void post_randomize();
    $display("a-%d b-%d p-%d",a,b,p);
  endfunction
endclass