 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
mathewthe2 Grandmaster Cheater
Reputation: 0
Joined: 28 Aug 2008 Posts: 562 Location: behind you... BOO!
|
Posted: Fri Jan 30, 2009 3:09 am Post subject: [Question] VB6 random code handling |
|
|
Ok I made a game in vb6, a fairly simple game, what I want to do is to let the computer pick a random number from variable(integer) x to y, and so I put the following code:
| Code: |
Dim x As Integer
Dim y As Integer
Dim computerpick As Integer
Dim aroundnumber As Integer
x = Val(maximumnumber.Caption)
y = Val(minimumnumber.Caption)
aroundnumber = x - y
computerpick = (Rnd()) *aroundnumber + y
Show.Caption = computerpick
|
And it worked fine, the computer picks a number between x and y(for example if x = 1, y = 20, it would choose 3,4,7,19 etc.)
According to theology, if
aroundnumber = x - y and computer pick = aroundnumber + y,
computer pick will equal to x - y + y(which is x), so I changed the code to
| Code: |
Dim x As Integer
Dim y As Integer
Dim computerpick As Integer
Dim aroundnumber As Integer
x = Val(maximumnumber.Caption)
y = Val(minimumnumber.Caption)
computerpick = (Rnd()) *x
Show.Caption = computerpick
|
which doesn't work and picked a number smaller than minimum, I wonder how does the random code(the number after * is the maximum the number could go)work.
Can anyone tell me I know you are reading this!
Also, I included the game in attachment, read instructions!
_________________
|
|
| Back to top |
|
 |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Fri Jan 30, 2009 5:48 am Post subject: |
|
|
this works because
| Code: | | computerpick = (Rnd()) *aroundnumber + y |
you have clearly found the difference, hence how small or big the random number can be, then added the minimum
this fails because
| Code: | | computerpick = (Rnd()) *x |
you completely ignored the minimum, this will give you a number between 0 and x, hence not helpful
| Code: | | computerpick = ((Rnd()) *x) + y |
that will work
if you want it even shorter
| Code: |
Show.Caption = ( (Rnd()) *(Val(maximumnumber.Caption) - Val(minimumnumber.Caption)) + Val(minimumnumber.Caption) ) |
more confusing but you dont need to make variables for things you only use once really
edit: found the hole
| Quote: | | aroundnumber = x - y and computer pick = aroundnumber + y |
computer pick = ((random value) * aroundnumber) + y, hence why you cant simplify it
_________________
|
|
| Back to top |
|
 |
mathewthe2 Grandmaster Cheater
Reputation: 0
Joined: 28 Aug 2008 Posts: 562 Location: behind you... BOO!
|
Posted: Fri Jan 30, 2009 9:12 am Post subject: |
|
|
| Snootae wrote: | this works because
| Code: | | computerpick = (Rnd()) *aroundnumber + y |
you have clearly found the difference, hence how small or big the random number can be, then added the minimum
this fails because
| Code: | | computerpick = (Rnd()) *x |
you completely ignored the minimum, this will give you a number between 0 and x, hence not helpful
| Code: | | computerpick = ((Rnd()) *x) + y |
that will work
if you want it even shorter
| Code: |
Show.Caption = ( (Rnd()) *(Val(maximumnumber.Caption) - Val(minimumnumber.Caption)) + Val(minimumnumber.Caption) ) |
more confusing but you dont need to make variables for things you only use once really
edit: found the hole
| Quote: | | aroundnumber = x - y and computer pick = aroundnumber + y |
computer pick = ((random value) * aroundnumber) + y, hence why you cant simplify it |
yes i know that code number2 has ignored minium, thank you for replying, now i know that there is brackets in between even if it is the same number in mathematics.
_________________
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|