Jump to content
Sign in to follow this  

randomize a string ?

Recommended Posts

Is it possible to randomize a string without home-brewing a function to do it?    


Can only integral data types be randomized, hence not strings?


How much memory is allocated when a string is declared?  Is a string just a dynamic array 'underneath'?

Share this post

Link to post
Share on other sites

No, you can;t randomize a string, but you can randomize a dynamic array of bytes. You will need to have a foreach constraint to keep each byte to the visible ASCII character set. You can easily cast an array of bytes to a string. Many system tasks do this for you implicitly.

Share this post

Link to post
Share on other sites
Here a small example of code:
First, an example to create a byte dynamic array from a string. The dynamic array of bytes contains the ASCII CODE number representation of each character. The advantage is that this can be for example be randomized but strings cannot be randomized.
(created doing e.g. 

    for(i=0;i<stringvar.len(); i++) begin 
    byte_din_array = {byte_din_array ,stringvar[i]}; //stringvar[i] will return empty byte if  the index would be beyond the string length
    //The advantage of using stringvar[i] instead of stringvar.atoi(i) is that 
    //the string can have all ASCII characters and not just numbers.
    //Disadvantage is that the byte contains the ASCII CODE "number" 
    //representation of the character and that is not human readable
Here is the example to convert the dynamic array of bytes back in a concatenated string. 
You may have used the previous dynamic array to be partly randomized (with constraints) inside an xfer or changed in post_randomize.
    function string convert_byte_array2string(byte stringdescriptionholder[]);
        automatic string temp_str="";
        automatic byte byte_temp;
        automatic string str_test;
        for ( int unsigned i = 0; i<stringdescriptionholder.size(); i++)  begin
            i=i;//debug breakpoint
            byte_temp = stringdescriptionholder[i];
            str_test = string'(byte_temp); //the "string cast" will convert the numeric ASCII representation in a string character
            temp_str = {temp_str,str_test};
        return temp_str;

If you want more information about strings i recommend to read the section 3.7 of the Systemverilog LRM (2012) . It is about the string data types and explain the built-in methods used with string data types.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...