# Randomization method call failing for random seeds

**URL:** <https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, system-verilog-constraints\
**Created:** [August 16, 2016, 11:23pm UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159 "2016-08-16T23:23:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![szy0014](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@szy0014](https://verificationacademy.com/forums/u/szy0014)\
**Post date:** [August 16, 2016, 11:23pm UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/1 "2016-08-16T23:23:31Z")

</div>

Hi All,

I came across a bit counting algorithm called Brian Kernighan algorithm and decided to use it in system verilog constraints as follows:

```verilog

  class A;
    rand bit[7:0] numb;
    function int hamWeight(bit[7:0] n);
      int count=0;
      while(n)begin
        n = n & (n-1);
        count++;
      end
      return count;
    endfunction : hamWeight

    constraint c_numb{
      hamWeight(numb) == 1;
    }
  endclass

```

When I try the above code, it worked the first time. But as I try to run several seeds, there are few seeds that were failing with a randomization error. The warning that I get is “The randaomize method call failed. These variables contribute to a set of conflicting constraints”.

Since I have only one constraint, I’m not sure where to look at and how there is a conflict. Did I do something wrong fundamentally? If so can you tell me how to fix this?

Thanks

P.S : I know that there are other ways to get random 1 hot numbers, but I was just trying this out as an exercise.

---

<div class="post-metadata">

**Author:** ![user60](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user60/32/1096_2.png) [@user60](https://verificationacademy.com/forums/u/user60)\
**Post date:** [August 17, 2016, 5:08am UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/2 "2016-08-17T05:08:22Z")

</div>

_In reply to [szy0014](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159):_

In SV, function calls inside a constraint impose an implicit variable ordering (See: [Verification Course Blog | VeriLog Courses | CVC](http://www.cvcblr.com/blog/?p=725)). So in your case a random value of “numb” is chosen FIRST and then your constraint is solved - imagine with “numb = 0” you could get a failure.

Regards  
Srini  
[www.go2uvm.org](http://www.go2uvm.org)

---

<div class="post-metadata">

**Author:** ![mayurkubavat](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mayurkubavat](https://verificationacademy.com/forums/u/mayurkubavat)\
**Post date:** [August 17, 2016, 5:35am UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/3 "2016-08-17T05:35:53Z")

</div>

_In reply to [szy0014](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/2):_

This uses the same concept…

```verilog
constraint c_numb{
   numb != 0; //Correction!
  (numb & (numb - 1)) == 0;
}

```

---

<div class="post-metadata">

**Author:** ![szy0014](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@szy0014](https://verificationacademy.com/forums/u/szy0014)\
**Post date:** [August 17, 2016, 5:37am UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/4 "2016-08-17T05:37:10Z")

</div>

_In reply to [Srini @ CVCblr.com](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/3):_

@Srini - Oh. I get it. So is there a workaround for this? Is there a way we can solve the constraint first?

@mayurkubavat - That seems to be working. Thanks. However, having a function is better in case we want to generalize the number of 1’s in a number (for example if we need a number with 2 hot bits instead of 1).

---

<div class="post-metadata">

**Author:** ![szy0014](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@szy0014](https://verificationacademy.com/forums/u/szy0014)\
**Post date:** [August 17, 2016, 6:01am UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/5 "2016-08-17T06:01:53Z")

</div>

_In reply to [szy0014](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/4):_

Actually, extending from the same idea, I think for 2 hot bits we can modify the constraint as follows:

```verilog
constraint c_numb{
  (numb & (numb-1) & (numb-2)) == 0;
}

```

So, I agree that this could be used in general.

But still please let me know if it is possible using the function. I curious to see if there is a solution for that.

Thanks

---

<div class="post-metadata">

**Author:** ![mayurkubavat](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mayurkubavat](https://verificationacademy.com/forums/u/mayurkubavat)\
**Post date:** [August 17, 2016, 6:03am UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/6 "2016-08-17T06:03:13Z")

</div>

_In reply to [szy0014](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/5):_

That’s correct! But the problem here is, as Srini explained, function call inside constraint will force dependent variables to be solved first and this can generate conflicting conditions.

There won’t be parallelism if functions are used in constraint… So, that restricts the use i guess.

Best way is,

```verilog
constraint c1{
  $countones(numb) == 1;
}

```

---

<div class="post-metadata">

**Author:** ![szy0014](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@szy0014](https://verificationacademy.com/forums/u/szy0014)\
**Post date:** [August 17, 2016, 3:51pm UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/7 "2016-08-17T15:51:57Z")

</div>

_In reply to [mayurkubavat](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/6):_

Can you explain how is the system function call different from the user defined function call? Because in some ways, all I was trying to do is implement the behavior of $countones in the class isn’t it?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 17, 2016, 4:30pm UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/8 "2016-08-17T16:30:19Z")

</div>

_In reply to [szy0014](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/7):_  
The constraint solver recognizes certain builtin SystemVerilog functions as mathematical expressions and expands them into the constraint equations. It cannot do that with user defined functions. (well, the LRM does not currently define a way to do that)

---

<div class="post-metadata">

**Author:** ![szy0014](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@szy0014](https://verificationacademy.com/forums/u/szy0014)\
**Post date:** [August 17, 2016, 5:27pm UTC](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/9 "2016-08-17T17:27:45Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/randomization-method-call-failing-for-random-seeds/32159/8):_

Oh Okay. Thanks Dave. But it sure would be a useful feature to add in future versions I think.
