
-----------------------------------
adrianr2z
Thu Apr 11, 2013 9:36 pm

Simulate a 100 times?
-----------------------------------
I need some help for a project. I need to make a 10 meter bridge that has a man standing in the middle. I have to make him randomly move left and right until he falls off.I then need to display how many steps it took him to fall off and display the average it would take him to fall of out of a 100 times without the animation. How would I do this? I have no clue where to start. Any help in guiding me on how to start this?

I have no clue how to run the program a 100 times in the background or where to even begin.

-----------------------------------
Nathan4102
Thu Apr 11, 2013 9:53 pm

RE:Simulate a 100 times?
-----------------------------------
Make a picture of a bridge and a man, and have him move randomly left or right every250 ms or so. Then once he's 5meters from the centre, count the number of steps. You can do this in the background by skipping the 250ms part and cutting all animations.

-----------------------------------
Panphobia
Fri Apr 12, 2013 12:29 am

RE:Simulate a 100 times?
-----------------------------------
A good start would be to learn about how to generate random numbers, and also how to loop.

-----------------------------------
Zren
Fri Apr 12, 2013 12:32 am

RE:Simulate a 100 times?
-----------------------------------
I'd focus on doing the simulating part first. Then worry about the graphical part.

This problem can be imagined as a number line.

http://www.mathematic.ws/wp-content/uploads/2009/04/number-line.png

You need a variable to store/remember the current location on the number line. You need to use a comparison operator to specify the invalid range on the number line. Example: The bridge's floor only exists between the range -3 to +3. So any position below -3 and above +3 would cause you to fall off the bridge.

-----------------------------------
Clayton
Fri Apr 12, 2013 8:03 am

RE:Simulate a 100 times?
-----------------------------------
To help out in perhaps a different way from Zren, I'll give you some really rough pseudo code which may help you see how one simulation should end up looking:


move the man until he falls
    take a step
    does he move left or right?
        left: move one interval to the left
        right: move one invterval to the right
    is the man off the bridge?
        yes: awesome, simulation done, it took {x} steps to fall off
        no: darn, keep going


-----------------------------------
adrianr2z
Fri Apr 12, 2013 3:53 pm

Re: Simulate a 100 times?
-----------------------------------
Thanks for all the help guys. I'm going to be using if statements to get this working but my instructor recommended I use nested loops. The only problem with that is how do I do the average of steps it takes him to fall off the bridge out of a 100 times? I really have trouble with that part.

-----------------------------------
Panphobia
Fri Apr 12, 2013 4:03 pm

RE:Simulate a 100 times?
-----------------------------------
For every step from left to right, increment 1 to a counter, when the man is off the bridge reset the position of where he is at, to the middle of the bridge, and then keep doing that 100 times, when all 100 times are done, you can just divide the result by 100 and you got the average.

-----------------------------------
adrianr2z
Fri Apr 12, 2013 7:49 pm

Re: Simulate a 100 times?
-----------------------------------


loop
    counter1 := counter1 + 1
    loop
        
        step1 := Rand.Int (-1, 1)
        if step1 = -1 then
            center1 := center1 - 50
            counter2 := counter2 + 1
        elsif step1 = 0 then
            center1 := center1 + 0
        elsif step1 = 1 then
            center1 := center1 + 50
            counter2 := counter2 + 1

            exit when center1 >= maxx or center1 