# Unable to use randc for uniqueness

**URL:** <https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446>\
**Category:** SystemVerilog\
**Tags:** Specific-randomization-in-system-verilog-class, SystemVerilog, constraints\
**Created:** [August 18, 2021, 5:00am UTC](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446 "2021-08-18T05:00:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganeshbchoudhary.gmail.com](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@ganeshbchoudhary.gmail.com](https://verificationacademy.com/forums/u/ganeshbchoudhary.gmail.com)\
**Post date:** [August 18, 2021, 5:00am UTC](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446/1 "2021-08-18T05:00:06Z")

</div>

I’m trying to solve Einstein’s riddle using system verilog constraints  
Here I see that all the constraints I have written in **einsten\_constraints** & **einsten\_constraint\_problem** are being followed but randc declaration in **einsten\_constraints** isn’t being followed its repeating because only member elements are treated as randc.

```verilog

typedef enum int { red,	green,	white, yellow, blue } color_e;
...

class einsten_constraints;

  randc color_e color;
...  
  constraint c1 ..
..  
  function void display();
..  
endclass

class einsten_constraint_problem;
   rand einsten_constraints ex[5];
   function new();
...
  constraint c4 { foreach(ex[i]) 
    				if(i>0)
    					(ex[i].color == color_e'(white)) <-> (ex[i-1].color == color_e'(green));  
                } 
  function void display();
..
endclass

program automatic top;
  einsten_constraint_problem ex_super;
  initial
    begin
      ex_super = new();
      if(ex_super.randomize())
      		begin
              $display("randomization succesful");
                ...
            end  
    end  
endprogram 

```

If i try to use below code in **einsten\_constraint\_problem** then its randomizing second time & constraints declared in **einsten\_constraint\_problem** wont be followed.  
If i dont want to use 2 foreach loops and check all possible ways for uniqueness, is there a way to solve this using randc

```verilog

....
einsten_constraints ex[5];
.....
  function void pre_randomize();
    foreach(ex[i])
      begin
        if(!(ex_c.randomize()))
          begin
        	ex[i] = ex_c ; 
          end
      end
  endfunction 
...

```

---

<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 18, 2021, 4:40pm UTC](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446/2 "2021-08-18T16:40:20Z")

</div>

_In reply to [GanBa](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446):_

randc does not guarantee uniqueness across multiple array elements. It only ensures that in successive randomizations, a different solution is provided until all possible solutions are exhausted. You only call randomize() once.

You might want to read [Redirecting…](https://blog.verificationgentleman.com/2014/07/fun-and-games-with-crv-zebra-puzzle.html)

---

<div class="post-metadata">

**Author:** ![ganeshbchoudhary.gmail.com](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@ganeshbchoudhary.gmail.com](https://verificationacademy.com/forums/u/ganeshbchoudhary.gmail.com)\
**Post date:** [August 18, 2021, 5:22pm UTC](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446/3 "2021-08-18T17:22:10Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446/2):_

> _In reply to [GanBa](https://verificationacademy.com/forums/t/unable-to-use-randc-for-uniqueness/39446/2):_  
> randc does not guarantee uniqueness across multiple array elements. It only ensures that in successive randomizations, a different solution is provided until all possible solutions are exhausted. You only call randomize() once.  
> You might want to read [Redirecting…](https://blog.verificationgentleman.com/2014/07/fun-and-games-with-crv-zebra-puzzle.html)

Got it. Thank you Dave.
