From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Random number generation

I have an array of length less than 10. The elements are integers between 0 and 50.

I want to generate a random number between 0 and 50 (all integers) excluding those in the array.

How do I do this in labview.

 

Thanks.

0 Kudos
Message 1 of 11
(5,751 Views)

snippet.png

 

But I'm not sure if this is what you mean.

 

 

Christian

0 Kudos
Message 2 of 11
(5,706 Views)

The code by Christian will have a smaller probability for the values 0 and 50 than for the other numbers. Your range covers 51 numbers so you have to multiply the random number by 51, then subtract 0.5 before rounding.

Also, it does not check if the number is in the array.

You could do something like this.

RndNum not in array.png

0 Kudos
Message 3 of 11
(5,695 Views)

This program generates nine random numbers andfill them in an array. But I didn't mean this.

 

I have an array A of nine elements whose elements are integers between 0 and 50.

I want to generate only one random number between 0 and 50 which doesn't match elements in A.

 

Thanks

0 Kudos
Message 4 of 11
(5,692 Views)
The random number generator outputs a value between 0 and 1, so multiplying the output of it by 50 and then rounding to the nearest integer and convert it to an integer datatype. This will give you a random number in the right range.

In terms of the comparison, there's a couple ways you can do it. First, under the array function palette there is a function called Search 1D Array that searches an array for a scalar value. If the index output is -1 the value wasn't found.

Second, you could use the Equal function and wire array to one side and the random number to the other then OR all the output elements to see if the value exists in the array.

Put all this logic in a while loop that runs until the random number generated is not in the array.

Mike....

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 5 of 11
(5,688 Views)

Here's what I would do.

 

 

0 Kudos
Message 6 of 11
(5,684 Views)

snippet.png

0 Kudos
Message 7 of 11
(5,680 Views)

Christian, your are still not generating integers (blue) and your random number are still not really random, because 0 and 50 only occur with half the probability of the other numbers. You would get fired from any casino in Vegas!. 😄

 

You need to multiply by 51 the round to -infinity as in my example above.

Message 8 of 11
(5,673 Views)
The problem with your example Christian is that you are comparing floating point values for equality - which will not always give you the result you are expecting..

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 9 of 11
(5,672 Views)

@mikeporter wrote:
The problem with your example Christian is that you are comparing floating point values for equality - which will not always give you the result you are expecting..

After rounding to nearest, we should be safe here. 😉

0 Kudos
Message 10 of 11
(5,666 Views)