Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Simulate a 100 times?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
adrianr2z




PostPosted: Thu Apr 11, 2013 9:36 pm   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




PostPosted: Thu Apr 11, 2013 9:53 pm   Post subject: 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




PostPosted: Fri Apr 12, 2013 12:29 am   Post subject: RE:Simulate a 100 times?

A good start would be to learn about how to generate random numbers, and also how to loop.
Zren




PostPosted: Fri Apr 12, 2013 12:32 am   Post subject: 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.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

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




PostPosted: Fri Apr 12, 2013 8:03 am   Post subject: 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:

pseudo:

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




PostPosted: Fri Apr 12, 2013 3:53 pm   Post subject: 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




PostPosted: Fri Apr 12, 2013 4:03 pm   Post subject: 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




PostPosted: Fri Apr 12, 2013 7:49 pm   Post subject: Re: Simulate a 100 times?

Turing:


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 <= 0
        end if
    end loop
    exit when counter1 = 100
end loop





So the above code is for the average part. I have a separate loop for just the graphics. The above part I use is to do it 100 times and get the average. However, the average seems to be very weird. I have it run 100 times and count the steps each time but when I divide the total number of steps by a 100, I usually get 2 or 3. It doesn't seem right and I dont know what I'm doing wrong.
Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




PostPosted: Fri Apr 12, 2013 7:54 pm   Post subject: RE:Simulate a 100 times?

You need to be resetting centre1 after each run.
adrianr2z




PostPosted: Fri Apr 12, 2013 8:03 pm   Post subject: Re: Simulate a 100 times?

Oh wow... I couldn't think of it.
It has more resonable averages now. It stead of 2 or 3 steps, it now displays 12 or 13 which sounds more resonable.
I was getting 600 or so steps but now I get like 1152 or around that.

Thanks a lot guys. I got my question solved.
adrianr2z




PostPosted: Fri Apr 12, 2013 9:40 pm   Post subject: Re: Simulate a 100 times?

Sorry for repost, but I am having a small little problem.
I have a picture that is suppose to exit when the x value is higher than maxx div 2 + 500 or less than maxx div 2 - 500.
The problem, the picture will go over those boundaries and still not quit... Here is the code.


Turing:

Draw.FillBox (0, maxy div 2 + 30, maxx, maxy, white)
Draw.Arc (maxx div 2, maxy div 2, maxx div 2 - 175, maxy div 2, 0, 180, 55)

Pic.Draw (left1, center, maxy div 2 + 30, picMerge)
delay (500)
loop

    step := Rand.Int (-1, 1)
    Draw.FillBox (0, maxy div 2 + 30, maxx, maxy, white)
    Draw.Arc (maxx div 2, maxy div 2, maxx div 2 - 175, maxy div 2, 0, 180, 55)
    if step = -1 then
        center := center - 50
        Pic.Draw (left1, center, maxy div 2 + 30, picMerge)
        counter := counter + 1

        delay (500)


    elsif step = 0 then
        center := center + 0


    elsif step = 1 then
        center := center + 50
        Pic.Draw (right1, center, maxy div 2 + 30, picMerge)
        counter := counter + 1
        delay (500)
       

    end if
exit when center >= maxx div 2 + 500 or center <= maxx div 2 - 500
end loop



Nathan4102




PostPosted: Fri Apr 12, 2013 9:48 pm   Post subject: RE:Simulate a 100 times?

Pictures don't just quit, you need to clear them using cls, or draw a white box over top of them.
adrianr2z




PostPosted: Fri Apr 12, 2013 9:52 pm   Post subject: Re: Simulate a 100 times?

no thats not what I mean, I have a white box drawn on it. I mean, when the picture's x value reaches the end of the bridge or the beginning of the bridge, I want the program to stop. This doesn't work for me:
"exit when center >= maxx div 2 + 500 or center <= maxx div 2 - 500"
Zren




PostPosted: Sat Apr 13, 2013 2:25 am   Post subject: RE:Simulate a 100 times?

When randomly choosing from a set of {-1, 0, +1} (aka Rand.Int(-1, 1), you can theoretically "randomly" choose 0 an infinite number of times. It might not actually happen in practice, but that doesn't mean you should include it in your range. Have a range with two possibilities, {0, 1} and set it to -1 when 0 pops up.

Try:
code:

if Rand.Int(0, 1) equals 1:
    thingy_direction = +1
else:
    thingy_direction = -1

thingy_x += direction


Also, why are you repeating yourself (Pic.Draw & delay). Also, why are you repeating yourself (Pic.Draw & delay). Instead of copy-pasting (or rewriting it), restructure your code so that common elements are reached in either condition.

adrianr2z @ Fri Apr 12, 2013 9:52 pm wrote:
no thats not what I mean, I have a white box drawn on it. I mean, when the picture's x value reaches the end of the bridge or the beginning of the bridge, I want the program to stop. This doesn't work for me:
"exit when center >= maxx div 2 + 500 or center <= maxx div 2 - 500"


Perhaps you should do some debugging on what value "center" is at. Your arc seems to be much smaller than a combined 1000 pixels (500px left + 500px right).


Turing:

    % Draw
    cls
    put minBound, " <= ", thingy_x, " <= ", maxBound
    % ...

    % Wait
    delay (500)
    exit when not (minBound <= thingy_x and thingy_x <= maxBound)
adrianr2z




PostPosted: Sat Apr 13, 2013 11:00 am   Post subject: Re: Simulate a 100 times?

Thanks so much for your help! I don't get how the minimum bound and max boundaries are different than the one of the bridge. I'm very confused because my bridge 2 x points are maxx div 2 - 500 so I did that for the boundaries but when i did what you told me and debugged it, it was much different and I don't understand why.
Anyhow, I figured out the min boundaries and max boundaries and it works fine, however my animation is very choppy and just unrealistic. My stick man is moving forward just fine and then he jerks back to like the other side of the bridge and I don't get why. Here is the code:
** also, the reason i do the pic.draw and delay is because my stickman has to start at the center of the bridge.
code:
Draw.FillBox (0, maxy div 2 + 30, maxx, maxy, white)
Draw.Arc (maxx div 2, maxy div 2, maxx div 2 - 175, maxy div 2, 0, 180, 55)

Pic.Draw (left1, center, maxy div 2 + 30, picMerge)
delay (500)
loop
    Draw.FillBox (0, maxy div 2 + 30, maxx, maxy, white)
    Draw.Arc (maxx div 2, maxy div 2, maxx div 2 - 175, maxy div 2, 0, 180, 55)

    if Rand.Int (0, 1) = 1 then
        center := center - 100
        Pic.Draw (left1, center, maxy div 2 + 30, picMerge)
        counter := counter + 1
    else
        center := center + 100
        Pic.Draw (right1, center, maxy div 2 + 30, picMerge)
        counter := counter + 1

        delay (1000)



        exit when center >= maxbound or center <= minbound
    end if

end loop


put counter


If you run it, the character movement is not very smooth as well as it randomly jumps around the bridge. Sorry for all the stupid questions I am asking. [/syntax]
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: