Jump to content

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 =(

Link to comment

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.

Link to comment

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

Show us what you have tried, then we can help you.

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. ....

Link to comment

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

Link to comment

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

Link to comment

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.

Link to comment

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?

Link to comment

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.

Link to comment

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

MicrochipHo,

Obviously, there are a variety of ways to do this. There are indeed canned functions that perform the function you want and there is nothing wrong with knowing them. There is still something to be learned from doing it the hard way. You can learn a lot more about LabVIEW the hard way, IMO.

In a beginning programming class you will often encounter assignments where thay ask you to perform operations for which there are canned routines that save you from actually learning much about basic programming skills and functions. Really, that is the only reason to waste a lot of effort on the following method, but it has its place.

In the attached BD you have a loop that runs 10 times.

The random number is created between 0 and 1 which is scaled per your method so that the results can be sorted down to the range you want.

Then through the use of inequalities and the logical AND you produce the test result of whether the number is in the range you like or not.

You get to see how a shift register stores the results of prior cycles of the loop, and how that must be initialized so it starts empty every time you run the loop.

You get to see how the case structrue takes the test result and either builds the "in range" values to the shift register, or sends the shift register on to the next cycle unchanged.

And you get to see a visual comparison of the raw data and the processed data to make sure the results are as they should be - a good debugging technique.

If you turn on the Highlighting (lightbulb icon) when you run this you can get an easy to follow beginning lesson in how dataflow. programming works.

http://lavag.org/old_files/monthly_04_2008/post-48-1208803913.jpg' target="_blank">post-48-1208803913.jpg?width=400

Link to comment

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.

Link to comment

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.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.