Generate random return less than 0.01 always

Hello,

I desired to implement random: Pass or Fail, code.
So I generate random and store in on fRand.
fRand = GenerateRandomNumber();
if(fRand<0.5)then
//Pass
else
//Fail
Endif

But fRand always return less than 0.01. — this is very weird, I know this is still random, but is still not proportion.

Any other way to produce random number?

How about I generate a high frequency sine wave, and generate a delay statement,
where ever the position when delay stop, that would be the value…

LOL. This will make my life more difficult.

Sorry to ans my own question… wth.

This is the code I use to produce random numbers that have some solid offset.
You can mess with the offset numbers and get them to where you need them.

random_number_1 = (((GenerateRandomNumber() * 11.0)) + 22.9 * 2.1685);
random_number_2 = (((GenerateRandomNumber() - 0.5) / 1.2) + 38.9 * 0.1685);

Are you calling SeedRandomNumber() in your strategy?

Hi Philip, I have few Generate Random command on other charts that are running concurrently.
But this one is interesting. It always generate less around 0.00nn

Yes, I tried to call SeedRandomNumber(), but results did not change.

I will try to use fRandom = sine (GenerateRandomNumber()); I am still thinking how the formula should look like to increase frequency (shorten the period).

Some sine wave code to get you started (we use it in our demos).

 Wave_TimeMs = Wave_UpTimer * 1000;

 Wave_SineOf = (Wave_TimeMs % (Wave_Cfg_PeriodSec*1000)) / (1000.0 * Wave_Cfg_PeriodSec) * 2.0 * 3.14159;

 Wave_SineValue = Sine(Wave_SineOf);

 Wave_CurrentValue = Wave_Cfg_Base + Wave_Cfg_Delta * Wave_SineValue;

So the others work properly and this one doesn’t? Are you sure fRand is not getting changed in another chart? Something isn’t right. Can you run just his code and disable the rest of your charts and see if it works?

Look At this:
for random length, i expect it to be 100 to 1000.
image
for random recipe:
image
for pass and fail:
image
The Gen Value is return from GenerateRandomNumber Command. It always stays on 0.00nn

Looks like the random recipe is working… others not.
I might use the sine function.
But GenerateRandomNumber is very weird, maybe because I am using softpac installed on Azure Cloud Virtual Windows?

Can you do something simple like this:

for n=0 to 49 step 1
  floattable[n] = GenerateRandomNumber();
next

and see what comes back?

Hi Philip,

Please observer first index. - I ran this command between 80 to 120 sec, random in between.

I think what is happening, the first “generation” is always 0.00nn area…
on 3rd generation onwards, it becomes random.

This is why, Random Recipe code is random, from observation… because Random Recipe code is generated after few other random variables…

Looks like you found a bug.

Which Version of SoftPAC are you running? I am getting the same first low number result on 9.5g, but it looks like 10.0d works okay.

Softpac version R10.0f

Let me submit this to support team.

Thank you very much.

I too have this problem and need this requirement.

I would like to generate random number between
1 to 1000
-1000 to 1000
-100.000 to 100.000

Could someone help to provide the OptoScript. Thank you

If you take a look at the PAC Control command reference (included in PAC Control on your computer via the help menu, or from our website) you can see some details and hints about the random number.

From here you can do the math you need to get the range of numbers you need.
Try putting a few variables in and changing them in debug mode.
You will quickly find the offset you need to get the range of numbers are you looking for.

I saw this in help file. Thank you for your help and sharing this information.

Does this help you get started?
I put this code together that multiplies by a scaling value, then uses offsets to get the correct ranges and allow for negative numbers. I also added rounding since it looks like you just want integers for the first two ranges, but 1000th’s on the third.

// +1 to +1000.
random[0] = Round(GenerateRandomNumber() * 1000) + 1;

// 0 to +2000, offset -1000 for -1000 to +1000.
random[1] = Round(GenerateRandomNumber() * 2000) - 1000;

// 0 to 200, offset -100, multiply by 1000 and round to get three extra places, then divide back to -100.000 to +100 range.
random[2] = Round((GenerateRandomNumber() * 200 - 100) * 1000) / 1000;
1 Like