What is the best way to get a randomized real value?

Hi,
In a task I want to get randomized real numbers between 16 and 20. What is the best way to do it?(range should be configurable real number should be inside [A:B])

With regards,
Tarun

In reply to perumallatarun:

Hi,

Verilog/System verilog has not any inbuilt method to do randomization of real numbers.
But you can use following conversion functions to achieve your intended functionality.
1).$bitstoreal()
2).$realtobits()

*In reply to perumallatarun:*Some tools already support this. Check with your vendor.

Otherwise you can do

class C;
  rand int unsigned multiplier;
  real result, A, B;
 
  function post_tandomize();
     result = A + (B-A)*(real'(multiplier)/32'hffffffff)
  endfunction
endclass


1 Like

Hi ,

We can randomize using $urandom command and if the data has pre defiend range , we can use $urandome_range( ) function . IS this the best way ? I am not sure but it works quite well and gives randomized value at every clock edge if used with clocking block

In reply to prashish_143:

$urandom and $urandom_range only returns integers. If you want fractional results, you need to scale the returned value.

In reply to dave_59:

Thank you Dave.

Hello !

a. For example I just tried declaring a variable as given below and applied the constraints. Things were working fine, but I see people posting we can’t use rand real and constraints.

b. Is the below coding style accepted to generate random real numbers ? Or is it just the tool supported format. [Tried running in cadence simulator]


 class volt;
   rand real voltage;
 
   constraint voltage_c {
     voltage inside {[1.5:2.5]}
   }
 endclass: volt

Share in your comments/thought process !

Thanks

*In reply to desperadorocks:*Yes, a number of tools have recently added support for real types where previously restricted to integrals. You need to contact your vendor for exactly what kind of features are supported as none of this is in the LRM.

I would assume that only constraints involving ranges or limits to non-random variables would be allowed here.

In reply to dave_59:

Thanks Dave ! Appreciate your response !

In reply to dave_59:


class C;
  rand longint unsigned multiplier;
  real result;
  rand int unsigned A, B;
  constraint condition1 {A<B;}
  constraint condition2 {multiplier >= (A*100); multiplier <= (B*100) ; solve B before multiplier;solve A before B;}                    
  function void post_randomize();
    result = real'(multiplier)/100.0;
    $display(A);
    $display(B);
    $display("%0d",multiplier);
    $display("%f",result);
    $display("\n");
  endfunction
endclass

module top;
  C c=new;
  initial begin
    repeat(2)
      assert(c.randomize);
  end
endmodule


I have used 100 because I wanted to have at least 2 numbers after the decimal.
Can I call it a reasonable solution or is there any case to take care of?