# Generate a random # between 18 and 24, return that number to a separate

## Recommended Posts

Ok, Part of the assignment is to generate a number between 18 and 24, and then i want to return that number to a sub VI. What a I did is multiply the randome # generator by a 100, and then put a conditional if the number is greater than 18 and less than 24....can't return the actual value, because than the output is a boolean. There are examples of how to do this whole program on the internet, but I want to work with my own code. How do I get this to return the values in float precision? I know I sound lame but I am terrible at programming...but i am trying =(

QUOTE (MicrochipHo @ Apr 21 2008, 09:40 AM)

I know I sound lame but I am terrible at programming...but i am trying =(

QUOTE (MicrochipHo @ Apr 21 2008, 08:40 AM)

Ok, Part of the assignment is to generate a number between 18 and 24, and then i want to return that number to a sub VI. What a I did is multiply the randome # generator by a 100, and then put a conditional if the number is greater than 18 and less than 24....can't return the actual value, because than the output is a boolean. There are examples of how to do this whole program on the internet, but I want to work with my own code. How do I get this to return the values in float precision? I know I sound lame but I am terrible at programming...but i am trying =(

The random number generator generates a number between 0 and 1. You need to cover a range of 6 (24-18) so multiply the random number by 6 and you have a random number between 0 and 6. Now add 18 and you're there.

QUOTE (Chris Davis @ Apr 21 2008, 04:42 PM)

Update: I tried doing this: two random # generators, one times 18, one times 24. Subtracted them, and returned that to a for loop (I disabled the indexing), and returned it to the Celsius to Farinheight. Am i going to have to put that into an array. What I want it to do is to stop collecting the 18-24 data and stop converting it, stop writing it to the array and etc... plan to do that with a while loop and/or case structure. ....

QUOTE (MicrochipHo @ Apr 21 2008, 11:40 AM)

Ok, Part of the assignment is to generate a number between 18 and 24, and then i want to return that number to a sub VI. What a I did is multiply the randome # generator by a 100, and then put a conditional if the number is greater than 18 and less than 24....can't return the actual value, because than the output is a boolean. There are examples of how to do this whole program on the internet, but I want to work with my own code. How do I get this to return the values in float precision? I know I sound lame but I am terrible at programming...but i am trying =(

At least you are honest. Generally we don't lay out answers for people doing school work. But we will definitely give help if you have put in some effort yourself.

If you are more verbal than mathematical try writing out the process in words, using phrases like: if this is so, then do response 1...if this is not so, then do response 2.

This verbal function comes from the Case Structure. in LabVIEW. The responses are put within the Case Structure - True Case for when the test is so, and False when it is not so.

In this example when you have tested the number and it meets your criteria you simply wire the same data you tested into the Case structure and out the far side in the TRUE case, but not in the false.

The test is performed and the T or F drives which Case will run. All the case needs is the number to pass or reject. If the test is false since there is no data wire across the false case, then that number dies. If the case is TRUE then the data is wired through the TRUE case and you can pick it up on the output side of the Case to be dealt with as needed.

Within the TRUE and false cases you can do much more than simply pass the data or not. You could do all manner of complex things to the data in the false case, or all manner of different things in the TRUE case. The outputs can be different data streams. You could do further nested tests. For instance you could sort the false numbers, those which are greater that 24 to return the numbers that are greater than 24 AND greater than 50 but less than 75. Wire that to the output side of the case and you now have two results.

The case is "polymorphic" as well. It is not limited to TRUE and false cases. You could wire integers into the ? terminal and the cases would become case 1, 2, 3, 4 etc. Or it can be Error and No Error, and so on.

However you set it up, when it gets all its input data, it looks at the conditional (?) input and finds that corresponding case and runs it.

Mike

If MAX is your maximum desired value, MIN is your minimum desired value, and RAND is a random number between 0 and 1 then:

RAND * (MAX - MIN) + MIN

is a random number in the desired range.

QUOTE (Aristos Queue @ Apr 21 2008, 10:00 AM)

Which is what TobyD said above. Sometimes a formula is worth a hundred words (since a picture is worth a thousand). QUOTE (orko @ Apr 21 2008, 11:01 AM)

Which is what TobyD said above. Sometimes a formula is worth a hundred words (since a picture is worth a thousand). And, here's what that picture looks like  You can download this VI as part of the OpenG Numeric Tools library.

QUOTE (TobyD @ Apr 21 2008, 04:56 PM)

The random number generator generates a number between 0 and 1. You need to cover a range of 6 (24-18) so multiply the random number by 6 and you have a random number between 0 and 6. Now add 18 and you're there.

Thanks for all your help guys! Ok, I got the program to run. I am having trouble getting my thermometer to measure in real time. I am guessing the 1-10 loop isn't going to count for 10 seconds..... The assigment is to get the thermometer to run for 10 seconds, gathering a data point for every .25 seconds. I have a feeling that I put the time delay in the wrong place....and I am going to have to rearrage the whole thing.... After the array is written,it has to be output on a chart and written into a spreadsheet . After pressing stop, the dialog box will appear, and ask the user to name the spreadsheet, and the file should appear....let me know what you think

It's worth mentioning, here, that if you want an even distribution of integers, then the formula looks like this:

INT( RAND * ( ( MAX + 0.5 ) - ( MIN - 0.5 ) ) + ( MIN - 0.5 ) )

Otherwise, your MIN and MAX values will have half the rate of occurence (probability) of the values between them. FYI, we're planning on adding support for integers in a future release of the OpenG VI.

QUOTE (MicrochipHo @ Apr 21 2008, 11:17 AM)

The assigment is to get the thermometer to run for 10 seconds, gathering a data point for every .25 seconds.

HINT: If the loop is operating at a 0.25 second rate, then how many iterations will it take until you reach 10 seconds?

QUOTE (Jim Kring @ Apr 21 2008, 07:26 PM)

It's worth mentioning, here, that if you want an even distribution of integers, then the formula looks like this:

INT( RAND * ( ( MAX + 0.5 ) - ( MIN - 0.5 ) ) + ( MIN - 0.5 ) )

Otherwise, your MIN and MAX values will have half the rate of occurence (probability) of the values between them. FYI, we're planning on adding support for integers in a future release of the OpenG VI.

Thanks for the formula, I will keep that in the back of my mind until further use. the question remains is how to get it in real time, plot for 10 seconds every .25 seconds. and then to write it to a file. If anyone has any input by 3PM today, that'd be cool. I will keep on trying to figure crap out until then. See Vi in my latest post above.

mross,

Correct me if I'm wrong, but isn't there a possibility that none of the 10 iterations will produce a number in the desired range? I would think a while loop (vs. For Loop) that tests the size of the output array to ensure a set number of results would be nice.

I see what you are getting at, and this is a good learning exercise for someone learning LabVIEW. For real world applications I think you'd agree that a one-shot algorithm approach that gaurantees one number within range on each execution (rand*(MAX-MIN) + MIN) would be a better approach.

QUOTE (orko @ Apr 21 2008, 03:04 PM)

mross,

Correct me if I'm wrong, but isn't there a possibility that none of the 10 iterations will produce a number in the desired range? I would think a while loop (vs. For Loop) that tests the size of the output array to ensure a set number of results would be nice.

I see what you are getting at, and this is a good learning exercise for someone learning LabVIEW. For real world applications I think you'd agree that a one-shot algorithm approach that gaurantees one number within range on each execution (rand*(MAX-MIN) + MIN) would be a better approach.

Exactly right. And if you run this one you can watch it happening (hence the extra indicator). Also,very instructive to the new programmer.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×