NI Home > Community > NI Discussion Forums
Reply
Member
angoav98
Posts: 54
0 Kudos
Accepted Solution

¿there is another form to do this?

I have six random numbers. I need that the six numbers are different, too, i need that the number not greater than 45 and are integer.

Then, i need thet the six number not greater than 45 and are integers and that six number are different.

i attached my file .VI for labview 2009. My program is working  and meets the features that i need

 

 however, I would like to know if you have other form to do this.

Thanks

Active Participant X.
Active Participant
X.
Posts: 1,672

Re: ¿there is another form to do this?

[ Edited ]

I'd suggest this:

 

Array of Random Numbers.png

Knight of NI
RavensFan
Posts: 15,005
0 Kudos

Re: there is another form to do this?

[ Edited ]

Instead of using an entire screen full of OR functions repeatedly to get down to one boolean, you can use a Compound Arthimetic node and set it for OR.  Stretch the node as far as you need.  Multiple inputs, one node, one output.

 

You have some issues with your rounding.  As pointed at in your other thread, you should be rounding down.  When you do a round by nearest, you don't have uniform distribution of your random numbers.  You are going to get values from 0 to 45, but the values 0 and 45 have half the chance of being picked as the values from 1 through 44. Round down, you'll have an equal chance of getting 0 through 44.  If you need 1 through 45, then add 1 to the result.

 

 

I would do the random number generation in a loop.  As you find each number, build it into an array.  Search for the number in your array.  If the number exists, don't add it, iterate the loop and try again.  If it does exist, then build the new value into the array.  The loop ends once you have 6 numbers in your array.

 

Edit:  I see X came up with a very similar version while I was doing mine.

 

Member
angoav98
Posts: 54
0 Kudos

Re: there is another form to do this?

The two solutions is very well. Thaks!!.. I not know very labview, and this ... I learned a lot

Knight of NI
altenbach
Posts: 26,068

Re: ¿there is another form to do this?

[ Edited ]

You did not specify the lower limit, so I assume zero.

 

All the above solutions seem unecessarily complicated. All you need is the following. :smileyvery-happy:

 

 

(generate 0..45 integer array -> Riffle -> reshape to 6)

 

 


LabVIEW Champion . Do more with less code and in less time .

Knight of NI
altenbach
Posts: 26,068

Re: ¿there is another form to do this?


angoav98 wrote:

 however, I would like to know if you have other form to do this.


A few more comments on your code:

Your code will not work, because the random number generators are outside the loop and thus execute only once at the start of the program. All subsequent iteration will read the same value from the tunnel and nothing will ever change until you stop and restart the program. Either it succeeds immediately or fails forever. No loop needed.

 

Learn about arrays. Whenever you have all these identical operations, you can do it once on an array instead.

 

Your loop does absolutely nothing except spin as fast as the computer allows, consuming all available power of one CPU core. The code should complete once 6 are found, right? No user iteration needed.


LabVIEW Champion . Do more with less code and in less time .

Knight of NI
RavensFan
Posts: 15,005
0 Kudos

Re: there is another form to do this?

[ Edited ]

Why build an array in a loop when there is the ramp function?  :smileywink:

 

Knight of NI
altenbach
Posts: 26,068
0 Kudos

Re: there is another form to do this?

[ Edited ]

Ravens Fan wrote:

Why build an array in a loop when there is the ramp function?  :smileywink:



... because if the problem deals with integers, there should not be any orange wires! :smileysurprised:

 

Besides, my loop will be folded into a constant by the compiler because is can be computed fully at compile time.

The ultimate efficiency at run time :smileyhappy: The ramp subVI does not offer this.

Also the ramp needs three diagram input constants, my loop only one. This is simpler.


LabVIEW Champion . Do more with less code and in less time .

Trusted Enthusiast
Darin.K
Posts: 3,786
0 Kudos

Re: there is another form to do this?

Why build the array at all when the index output is right next door and you simply pass in an array initialized to the right size.
Knight of NI
altenbach
Posts: 26,068
0 Kudos

Re: there is another form to do this?


Darin.K wrote:
Why build the array at all when the index output is right next door and you simply pass in an array initialized to the right size.

I cannot really figure out which diagram you are talking about. :smileywink:


LabVIEW Champion . Do more with less code and in less time .