Computer Science Canada problem with random number |
Author: | rogerfrancis14 [ Thu Jun 01, 2006 3:58 pm ] |
Post subject: | problem with random number |
i have a small program with this program. i cannot find way to put in a number which is randomly chosen. pls help me. the program does not work when i put in a random number. var m : int %value of random number randint (m, 1, 100) % The "Drag" program. procedure DrawThemaple (x, y : int) var pic : int := Pic.FileNew ("Picture5.jpg") %getting the picture Pic.Draw (pic, 40 + x + m, 54 + y + m, picCopy) delay (50) var pic1 : int := Pic.FileNew ("Picture00.jpg") %getting the picture Pic.Draw (pic1, 41 + x + m, 54 + y + m, picCopy) end DrawThemaple %drawing outline of the flag drawmapleleaf (60, 280, 130, 360, 12) drawbox (20, 280, 45, 360, 12) drawbox (150, 280, 175, 360, 12) var xBox : int := 0+m var yBox : int := 0+m var x, y, diffX, diffY, btn, updown : int var buf : array 1 .. sizepic (0+m, 0+m, 40 + m, 40 + m) of int takepic (xBox + m, yBox + m, xBox + m + 40, yBox + m + 40, buf) %record the pixel values DrawThemaple (xBox + m, yBox + m) for i : 1 .. 75 if buttonmoved ("down") then buttonwait ("down", x, y, btn, updown) if (xBox + m) <= (x+m) and (x+m) <= (xBox + m + 60) and (yBox + m) <= (y+m) and (y+m) <= (yBox + m + 70) then % The mouse has been clicked in the box diffX := (x + m) - (xBox + m) %finding the difference diffY := (y + m) - (yBox + m) %finding the difference loop mousewhere (x, y, updown) exit when updown = 0 if (xBox + m) not= (x+m) - (diffX + m) or (yBox + m) not= (y+m) - (diffY + m) then drawpic (max (xBox + m, 0+m), max (yBox + m, 0+m), buf, 0+m) %copy picture onto screen xBox := xBox + m yBox := yBox + m xBox := (x+m) - (diffX + m) yBox := (y+m) - (diffY + m) takepic (max (0+m, xBox + m), max (0+m, yBox + m), %record the pixel values min (xBox + m + 40, maxx), min (yBox + m + 40, maxy), buf) DrawThemaple (xBox + m, yBox + m) %drawing the picture end if %drawing outline of the flag drawmapleleaf (60, 280, 130, 360, 12) drawbox (20, 280, 45, 360, 12) drawbox (150, 280, 175, 360, 12) end loop end if end if DrawThemaple (xBox+m, yBox+m) end for %///////////////////////////////////////////////////////////////////////////////////[img][/img] thanks |
Author: | Cervantes [ Thu Jun 01, 2006 4:08 pm ] |
Post subject: | |
I really don't want to have to download 3 files, make sure their all in the same folder, run the code, and examine it just to offer help. Things are a lot easier if you post the relavent section of your code and describe the problem in words. Also, please use [ code][ /code] tags when posting code. You should be using Rand.Int for creating random numbers. randint is horrible coding practice, because you're giving your variable to a procedure that really should never get that variable in the first place. What's more, that procedure is given full access to assign whatever value it wants to that variable. |
Author: | rogerfrancis14 [ Thu Jun 01, 2006 8:11 pm ] |
Post subject: | |
hey thanks for the reply. the "dragging" file is the program that i put on top and the the other two are the pictures that i use in the progra. the problem for me is that in the program when i randomize the number i cannot use the mouse to drag the picture around. but if i don't use [i]Rand.Int[/i] of [i]randint[/i] the program works perfectly fine. i need to be able to make sure that each time the image comes on the screen it comes on a different position every time the program is run. thanks |
Author: | rogerfrancis14 [ Thu Jun 01, 2006 8:13 pm ] |
Post subject: | |
i made a small mistake in my second paragraph: the problem for me is that in the program when i randomize the number i cannot use the mouse to drag the picture around. but if i don't use (Rand.Int) or (randint) the program works perfectly fine. thanks |
Author: | [Gandalf] [ Fri Jun 02, 2006 11:48 pm ] |
Post subject: | |
Where are you using Rand.Int()? In the example you posted above, you assign it to a variable m which you then use everywhere in your program. I have no idea what it is doing, especially since it is not called anything descriptive at all... So how am I supposed to help you? If you only want the starting position to change each time the program is reset, you will only be using a random value in two spots: the starting x coordinate, and the starting y coordinate. |