Random values in simulations. New Components available!

For those who have tried to use the Random() function in QSpice you may not have all the functionality you’d prefer.

Observations:
Here is a simple example of using the Random() as a input to a B-source. It is the file “rand_QSpice_test.qsch” in the attached archive of files.

Sim plot:

You’ll notice that the random value output of B1 doesn’t change across the entire sim time of 1 second.
You’ll also notice that each of the 10 steps executed have a different starting value. This is because I feed the step_val into the Random() as an input. This forces a different seed value.

However, this starting value is exactly the same at each step every time I run the entire sim. IMHO, I don’t think this is very random. I even used the seed and seedclock options. Still the same starting points. At least it is consistent across multiple sim runs. If that’s what you want.

Unless I’m missing something, this is what is provided natively in QSPice (or other Spice programs.)
Please reply with a correction if you have more information.

UPDATE: Here’s a link to @KSKelvin post who may have solved this issue. Monte Carlo analysis - #7 by lpoma

NEW COMPONENTS
I’ve created three new components using C-blocks (with symbols). You may find these components useful mostly in transient sims.

Here is the zip archive with an .txt extension. When you download it, remove the .txt extension.
rand.zip.txt (37.3 KB)

The new components are C-blocks that generate a bound-defined output. A new random number is generated on every timestep of the sim. It can be used to simulate noise or other needs for a linear distribution of random numbers across the defined bounds.

rand_i
The first component is “rand_i”. It is an signed integer-only output. You define the Lower Bounds (lb) and the Upper Bounds (ub) value. Parameter substitution can be used on the component such as stepping.

Here are the files in the archive that apply for this component:

  • rand_i.cpp => C++ source code.
  • rand_i_dll => compiled C++ .dll code.
  • rand_i_qsym => symbol for the component. Useful for the “Symbol & IP Browser”
  • rand_i_test.qsch => test sim for this component. This sim does lb and ub stepping.

Note: The component flags and abort the step/sim if the lb >= ub. It is illegal.

Here is an example plot of the first step with the out and LB and UB bounds.

rand_d
The component is “rand_d” operates like the “rand_i” but uses double (floating) output. This means it can output signed fractional values.

You define the Lower Bounds (lb) and the Upper Bounds (ub) value. Parameter substitution can be used on the component such as stepping.

Here are the files in the archive that apply for this component:

  • rand_d.cpp => C++ source code.
  • rand_d_dll => compiled C++ .dll code.
  • rand_d_qsym => symbol for the component. Useful for the “Symbol & IP Browser”
  • rand_d_test.qsch => test sim for this component. This sim does lb and ub stepping.

Note: The component flags and abort the step/sim if the lb >= ub. It is illegal.

Here is an example plot of the third step with the out and LB and UB bounds.

rand_dx
The component is “rand_dx” operates like the “rand_d” but uses two inputs for the lb and ub bounds.
The inputs allow for changing the bounds on-the-fly as the sim is running.

You define the Lower Bounds (lb) and the Upper Bounds (ub) value. Parameter substitution can be used on the component such as stepping.

Here are the files in the archive that apply for this component:

  • rand_d.cpp => C++ source code.
  • rand_d_dll => compiled C++ .dll code.
  • rand_dx_qsym => symbol for the component. Useful for the “Symbol & IP Browser”
  • rand_dx_test.qsch => test sim for this component. This sim uses B-sources to vary the bounds on-the-fly in the sim.

Note: The component flags and abort the step/sim if the lb >= ub. It is illegal.

Here is an example plot of out and the changing LB and UB bounds.

This components are free for anyone’s use. No warranty is expressed or implied.
Len

1 Like

To all,

I have a new archive I’ve submitted.

Nothing about the rand components have changed. However, I added a component datasheet for hopefully a better set of instructions.

Len
rand(V1.0b).zip.txt (962.0 KB)

Hi Ipoma, thanks a lot for your post !!
It has helped me a lot to start understanding how the random() function works in QSPICE.
Although the problem I’m trying to solve have similarities, they are distinct. However, your support would be greatly appreciated.

I’m trying to use the “random()” function in QSPICE to perform Montecarlo analysis of my circuits (I need uniform distribution generators as input).
Most of the times I will perform “.op” simulations and sometimes, rarely, “.tran”.
In both the cases I will set a step “run” parameter to run the simulation multiple times.
In case of “.tran” sims, I always need the generated random number at t=0 will be maintained constant for entire time duration.
Taking inspiration of your initial file, “rand_QSpice_test.qsch”, I’ve edited what you can find below:


…and here are the results for 2 consecutive sims of same circuit:

image
image

As you say in your text, the function “random(run)” generates the same starting value at each step every time I run the entire sim. Ok, I agree with you that it’s not a very random, but, considering a very high number foreseen for future use, I can accept it in first instance for my purpose. It can be seen for v(out) and v(out1) in above plots. Furthermore, both the related B1 and B3 sources are perfectly matched, allowing, it seems, a LOT-modelling available in PSPICE simulator.
At the same time, B2 source is defined as function of the same “random()” but with void argument. The contemporary presence of “.option seedclock” causes the associated v(out2) to be different at each step every time I run the entire sim (initial and following values are different for consecutive sims). Definition of B2 source-like and B1 source-like models at the same time should so allow to perform significant settings improvement.
The top solution, I know, is to generate v(out) as done for v(out2) (effective random generation) but with the ability to force v(out1) value equal to v(out) at each step. Have you some suggestion on how to do it?
Going further in my study, come some painful notes.
Since I need to generate random numbers, in both the above-said model types, for different electronic components like voltage/current sources, resistors, capacitors and so on … I’ve tried to define a couple of montecarlo functions “MCO(nom, tol, run)” & “MC(nom, tol)” associating them to V1-V3 sources and R1-R4 resistors value. In the picture, last attempt is reported for resistances variation definition (v(out) & v(out1) directly).
=> Unfortunately, nothing similar to what expected is generated on “x”, “x2”, “y” and “y1”. Almost all are forced to 0 value! Some of you have some suggestions to solve this problem?

Many thanks.

@ToposKy ,

Check out this thread about similar issues using the random() funtion:
https://forum.qorvo.com/t/monte-carlo-analysis/15199/7

Len