LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

I am a new user

I working on one of the Courses and got stuck on one of the excercises, here is the problem

Problem 4
Build a VI that simulates the roll of a die with possible values 1 through 6 and records the number of times that the die rolls each value. The input is the number of times to roll the die, and the outputs include the number of times the die falls on each possible value. Use only one shift register.

Is there a solution to this out there? I have been working on it way too long and would like to see how this is done.

Thanks
0 Kudos
Message 11 of 25
(3,017 Views)
Use the random number VI.
It gives a result between 0 and 1, so to make that 1 to 6, we will multiply the outcome by 5 and round to the nearest number (so you get 0..5). Then, add 1.
Now that you have the number, we need to keep track using a single SR.
Create a 1D array with 6 elements initialized to 0 and wire it into the left side of the SR. Now, you need to take the "random" element of the array and increment it by 1.
Use index array and replace array to do this, and wire the result into the right side of the SR.
That's it.
If you want the die to be cast only when the user clicks a button, use a case structure or an event structure.

___________________
Try to take over the world!
0 Kudos
Message 12 of 25
(3,004 Views)
Two corrections.
First, there is no need to add the 1 since we want 0 to 5 and not 1 to 6.
Second, after building this and running it, I realized I have a mistake - rounding doesn't give a random result because 0 and 5 are less likely to be selected.
What you should do instead is multiply by 6 and round down (round to -infinity). This will gurantee that every number gets an equal opprotunity.

Message Edited by tst on 04-21-2005 10:50 PM


___________________
Try to take over the world!
0 Kudos
Message 13 of 25
(3,004 Views)
One more correction: If you get 1 from random number VI, you will get 6, which will not be rounded down to 5, so you need to coerce the result if you want to get a real result.

___________________
Try to take over the world!
0 Kudos
Message 14 of 25
(2,996 Views)
The area I'm having a problem with is keeping tack of how many times each value is "rolled". If you have a working example please post it, this driving me a little nuts!!
0 Kudos
Message 15 of 25
(2,994 Views)
Here.
Try rebuilding this yourself.
The array holds a cell for each value, keeping in that cell the number of times that value was cast.

___________________
Try to take over the world!
Message 16 of 25
(3,004 Views)
That is much closer than any thing I wired up, but I don't see results that simulate the rolling of a die.
I'm "rolling" 0's and 5's. I cheated a litle and added a SR to track what is being rolled. Here is your vi.
0 Kudos
Message 17 of 25
(2,992 Views)
I'm not sure what your problem is. The way you built it, it works fine.
If you want to see the results while it's running, place the arrays inside the loop.
To see that you're getting correct results, change the wait to 0. Now it will run faster and you can see that the numbers are more or less equal for each side.

Now, There is only one problem with your "cheat" - using the insert into array requires a lot of resources, because the memory for the array needs to be reallocated every time you make the array bigger. Remove that part, and you will see it runs much much faster.

After going back to the original exercise, I see that you need to roll X times, so make a numeric control, replace the while loop with a for loop and wire the numeric into N. Since this isn't user dependent, you can also remove the wait completely.

___________________
Try to take over the world!
0 Kudos
Message 18 of 25
(2,971 Views)
You don't understand that using a randomw number range of 0 to 5 makes it simpler to index the array. Arrays in LabVIEW (and most programming languages) is 0 based. The first element is at element 0. Element 0 holds the number of times the first number is generated, element 1 holds the number of times the second number is generated, etc. If the random number generator created 1 through 6, then you would have to subtract 1 before doing the index array. The way tst has done it, to display 1 through 6 to the user, increment the number. Look at the attached picture. It's your example modified with the increment and a better way to create the array of numbers generated.
Message 19 of 25
(2,957 Views)
Thanks guys,

I know it was a simple problem it is great that you all take time out to help us rookies..
0 Kudos
Message 20 of 25
(2,804 Views)