Generating random values in increasing order

In reply to dave_59:

A couple of further comments:

  • Your original intent can be implemented easily using the post_randomize hook to save the most recent randomization value. Something like this:
int last_value=0; // or whatever your minimum should be
constraint ascending { at > last_value; }
function void post_randomize(); last_value = at; endfunction

post_randomize is called automatically just after each randomization, so it will capture the just-randomized value ready to use in the constraint for the next attempt.

  • Dave’s point about setting limits on the increment is important. Without it, you’ll get all the values bunched-up in the upper part of your available range, which is probably not what you want.
  • Many thanks to Dave for sharing a very elegant solution (which I haven’t seen mentioned elsewhere) for something I asked for years ago: the ability to use the current value of a rand variable as a state value in a constraint. Unfortunately, tool support for const-cast is very limited at present. I’m also just a little bit worried about Dave’s interpretation of the LRM here. It’s not at all obvious to me that the rule he quotes is sufficient to allow for the use of const-cast in a constraint. After all, you wouldn’t normally expect to be able to declare a variable within a constraint, and that’s effectively what const-cast does.