Weighted randomization based on runtime data in System Verilog












0















Is there a way to do weighted randomization in System Verilog based on runtime data. Say, I have a queue of integers and a queue of weights (unsigned integers) and wish to select a random integer from the first queue as per the weights in the second queue.



int data[$] = '{10, 20, 30};
uint_t weights[$] = '{100, 200, 300};


Any random construct expects the weights hardcoded as in



constraint range { Var dist { [0:1] := 50 , [2:7] := 50 }; } 


But in my case, I need to pick an element from an unknown number of elements.



PS: Assume the number of elements and weights will be the same always.










share|improve this question



























    0















    Is there a way to do weighted randomization in System Verilog based on runtime data. Say, I have a queue of integers and a queue of weights (unsigned integers) and wish to select a random integer from the first queue as per the weights in the second queue.



    int data[$] = '{10, 20, 30};
    uint_t weights[$] = '{100, 200, 300};


    Any random construct expects the weights hardcoded as in



    constraint range { Var dist { [0:1] := 50 , [2:7] := 50 }; } 


    But in my case, I need to pick an element from an unknown number of elements.



    PS: Assume the number of elements and weights will be the same always.










    share|improve this question

























      0












      0








      0








      Is there a way to do weighted randomization in System Verilog based on runtime data. Say, I have a queue of integers and a queue of weights (unsigned integers) and wish to select a random integer from the first queue as per the weights in the second queue.



      int data[$] = '{10, 20, 30};
      uint_t weights[$] = '{100, 200, 300};


      Any random construct expects the weights hardcoded as in



      constraint range { Var dist { [0:1] := 50 , [2:7] := 50 }; } 


      But in my case, I need to pick an element from an unknown number of elements.



      PS: Assume the number of elements and weights will be the same always.










      share|improve this question














      Is there a way to do weighted randomization in System Verilog based on runtime data. Say, I have a queue of integers and a queue of weights (unsigned integers) and wish to select a random integer from the first queue as per the weights in the second queue.



      int data[$] = '{10, 20, 30};
      uint_t weights[$] = '{100, 200, 300};


      Any random construct expects the weights hardcoded as in



      constraint range { Var dist { [0:1] := 50 , [2:7] := 50 }; } 


      But in my case, I need to pick an element from an unknown number of elements.



      PS: Assume the number of elements and weights will be the same always.







      constraints system-verilog verification hdl random-seed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 6:01









      KrishnaKrishna

      6021515




      6021515
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Unfortunately, the dist constraint only lets you choose from a fixed number of values.



          Two approaches I can think of are




          1. Push each data value into a queue using the weight as a repetition count. In your example, you wind up with a queue of 600 values. Randomly pick an index into the queue. The selected element has the distribution you want. An example is posted here.


          2. Create an array of ranges for each weight. For your example the array would be uint_t ranges[2]'{{0,99},{100,299},{300,599}}. Then you could do the following in a constraint



            index inside {[0:weights.sum()-1]};
            foreach (data[ii])
            index inside {[ranges[ii][0]:ranges[ii][1]} -> value == date[ii];








          share|improve this answer
























          • Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

            – Krishna
            Nov 18 '18 at 16:36













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358317%2fweighted-randomization-based-on-runtime-data-in-system-verilog%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Unfortunately, the dist constraint only lets you choose from a fixed number of values.



          Two approaches I can think of are




          1. Push each data value into a queue using the weight as a repetition count. In your example, you wind up with a queue of 600 values. Randomly pick an index into the queue. The selected element has the distribution you want. An example is posted here.


          2. Create an array of ranges for each weight. For your example the array would be uint_t ranges[2]'{{0,99},{100,299},{300,599}}. Then you could do the following in a constraint



            index inside {[0:weights.sum()-1]};
            foreach (data[ii])
            index inside {[ranges[ii][0]:ranges[ii][1]} -> value == date[ii];








          share|improve this answer
























          • Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

            – Krishna
            Nov 18 '18 at 16:36


















          1














          Unfortunately, the dist constraint only lets you choose from a fixed number of values.



          Two approaches I can think of are




          1. Push each data value into a queue using the weight as a repetition count. In your example, you wind up with a queue of 600 values. Randomly pick an index into the queue. The selected element has the distribution you want. An example is posted here.


          2. Create an array of ranges for each weight. For your example the array would be uint_t ranges[2]'{{0,99},{100,299},{300,599}}. Then you could do the following in a constraint



            index inside {[0:weights.sum()-1]};
            foreach (data[ii])
            index inside {[ranges[ii][0]:ranges[ii][1]} -> value == date[ii];








          share|improve this answer
























          • Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

            – Krishna
            Nov 18 '18 at 16:36
















          1












          1








          1







          Unfortunately, the dist constraint only lets you choose from a fixed number of values.



          Two approaches I can think of are




          1. Push each data value into a queue using the weight as a repetition count. In your example, you wind up with a queue of 600 values. Randomly pick an index into the queue. The selected element has the distribution you want. An example is posted here.


          2. Create an array of ranges for each weight. For your example the array would be uint_t ranges[2]'{{0,99},{100,299},{300,599}}. Then you could do the following in a constraint



            index inside {[0:weights.sum()-1]};
            foreach (data[ii])
            index inside {[ranges[ii][0]:ranges[ii][1]} -> value == date[ii];








          share|improve this answer













          Unfortunately, the dist constraint only lets you choose from a fixed number of values.



          Two approaches I can think of are




          1. Push each data value into a queue using the weight as a repetition count. In your example, you wind up with a queue of 600 values. Randomly pick an index into the queue. The selected element has the distribution you want. An example is posted here.


          2. Create an array of ranges for each weight. For your example the array would be uint_t ranges[2]'{{0,99},{100,299},{300,599}}. Then you could do the following in a constraint



            index inside {[0:weights.sum()-1]};
            foreach (data[ii])
            index inside {[ranges[ii][0]:ranges[ii][1]} -> value == date[ii];









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 16:29









          dave_59dave_59

          19.8k21437




          19.8k21437













          • Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

            – Krishna
            Nov 18 '18 at 16:36





















          • Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

            – Krishna
            Nov 18 '18 at 16:36



















          Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

          – Krishna
          Nov 18 '18 at 16:36







          Thank you dave_59, for confirming. I think creating such huge arrays would be time-consuming and wastage of memory, I think implementing a custom weighted random function based on $urandom/$urandom_range would be optimal. What do you think?

          – Krishna
          Nov 18 '18 at 16:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358317%2fweighted-randomization-based-on-runtime-data-in-system-verilog%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word