LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Generate a random number in LabWindows 5.5

How do I generate a random number in LabWindows CVI Shahid F. Khalid gives the code "rand/(2*327.62)" in his book "LabWindows/CVI Programming For Beginners" page 47. This code gives errors
0 Kudos
Message 1 of 12
(9,049 Views)
You have the ansi C rand()
--> This function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.

The programmer's toolbox Random()
--> Returns a random number between Minimum and Maximum

The Analysis library GaussianNoise() and WhiteNoise() and probably some more...


For your code sample you probably forgot to #include
--
Guillaume Dargaud
http://www.gdargaud.net/
"Entropy isn't what it used to be."
Message 2 of 12
(9,046 Views)
rand is a function, so make sure to use parenthesis. It should be "rand()/(2*327.62)"

Luis
NI
0 Kudos
Message 3 of 12
(9,050 Views)
the easiest way to get a consistently random number is to call SetRandomSeed(0) (passing 0 indicates the function to use the current time) and them use the Random() with the desired range.

If you are using a previous version of CVI 7.0 remember to include the Programmer's toolbox in you project. Just add to your project the file C:\Program Files\National Instruments\CVI70\toolslib\toolbox\toolbox.fp and you should be able to run these functions.

Regards,

Juan Carlos
N.I.
0 Kudos
Message 4 of 12
(9,049 Views)
Here is a very simple example of generating random numbers.

You can modify it to suit your needs.

JLV

🙂

#include "stdtst.h"
#include "tsutil.h"
#include
#include
#include


void __declspec(dllexport) __stdcall PassFailTestResult(int *result,
char reportText[1024], short *errorOccurred, long *errorCode, char errorMsg[1024])
{
int error = 0;
int r;
srand (time(NULL));
// ErrMsg errMsg = {'\0'};
// ERRORINFO errorInfo;

// REPLACE THE FOLLOWING WITH YOUR SPECIFIC TEST CODE

// Boolean success = TRUE;
// char *lastUserName = NULL;

r = rand();
printf ("\nRandom Number is = %6d", r);
system("pause");


if (r > 15000)

// if (success)
*result = 1;
else
*result = 0;

// The following code shows how to access a property or variable via the TestStand ActiveX API
// To use this code you must add a parameter "CAObjHandle seqContextCVI" to this function and pass
// a sequence context to it.
// tsErrChk (TS_PropertyGetValString(seqContextCVI, &errorInfo,
// "StationGlobals.TS.LastUserName",
// 0, &lastUserName));

Error:
// FREE RESOURCES
// if (lastUserName != NULL)
// CA_FreeMemory(lastUserName);

// If an error occurred, set the error flag to cause a run-time error in TestStand.
if (error < 0)
{
// *errorOccurred = TRUE;

// OPTIONALLY SET THE ERROR CODE AND STRING
// *errorCode = error;
// strcpy(errorMsg, errMsg);
}
}
0 Kudos
Message 5 of 12
(8,995 Views)
The html format removed the header files from the display..

And the EDIT Post function didn't work... 😞

So here they are...

JLV

🙂

#include "stdtst.h"
#include "tsutil.h"
#include < math.h >
#include < stdio.h >
#include < stdlib.h >
Message 6 of 12
(8,994 Views)
I just want to point out that you don't get truly random numbers from any of these techniques. At best, you get a psuedorandom sequence that will eventually repeat itself if you call the function enough times with the same seed value. In a lot of cases, this can be sufficient to fulfill the needs of the application but there are times when true randomness is required and you are not going to achieve true randomness in software.

To get truly random numbers, you must use something that is capable of generating true randomness such as the decay of radioactive particles. There are hardware solutions out there that will generate truly random numbers but they may be overkill depending on the application.

Just thought I would point this out as it is something that often gets missed or glossed over in discussions of generating "random" numbers on computers.
Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
0 Kudos
Message 7 of 12
(8,989 Views)
Thanks MJF,

You are correct.
The example presented was simply to show an example of how to implement the code in CVI.
It creates a true false type of response. Not the best mind you, but a glimpse of how to proceed with CVI code.

😄

JLV
Message 8 of 12
(8,986 Views)


@MJF wrote:
I just want to point out that you don't get truly random numbers from any of these techniques. At best, you get a psuedorandom sequence that will eventually repeat itself if you call the function enough times with the same seed value.




Actually...

You are more than correct MJF...

I placed the code within a loop which printed out the P-R numbers and the numbers repeated themselves, most probably due to having the same seed based on the timestamp.

This was amusing to see.. and I thought I'd share my observation.

😄

JLV
0 Kudos
Message 9 of 12
(8,982 Views)
This is basic Computer Science theory. All of these "random" number algorithms are based on psuedorandom sequence generation. By definition, a psuedorandom sequence will repeat itself. The repetition frequency is based upon the size of the sequence generator. The actual sequence of numbers is based on the polynomial used to generate the sequence as well as the seed used to start the generator.

This sort of algorithm generates a sequence that looks like white noise on a short time scale (compared to the overall sequence length). Such sequences are used in hardware applications such as spread-spectrum and frequency-hopping transmission systems. Because they do repeat in a deterministic manner, a signal modulated or hopped in such a way can be correlated and reconstructed from the psuedorandom sequence as long as you know the number of bits in use, the rate at which the signal is modulated or hopped by the sequence and the polynomial used to generate the sequence. (But that is getting deep into communications theory and not really all that relevant to this discussion).

All I am really saying is that you will have to be aware of the fact that the numbers generated are not truly random and will repeat in time. This may or may not be a problem with a particular application, you will have to decide that based upon what you are trying to do. For short runs of numbers using varying seed values, you can achieve sequences that behave like white noise. For longer runs of numbers, you either need to find a true random source or need to insert some sort of method to change seeds as you go. If you generate your numbers in response to user input, you can use a timestamp value as a seed in a fairly effective way as the exact time at which a user action occurs is as good as random for most applications.
Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
Message 10 of 12
(8,979 Views)