Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Question] VB6 random code handling

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
mathewthe2
Grandmaster Cheater
Reputation: 0

Joined: 28 Aug 2008
Posts: 562
Location: behind you... BOO!

PostPosted: Fri Jan 30, 2009 3:09 am    Post subject: [Question] VB6 random code handling Reply with quote

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!



The Extension 'rar' was deactivated by an board admin, therefore this Attachment is not displayed.


_________________
This is what you get if you scan a noob game with my -Mth2- Scanner-
Back to top
View user's profile Send private message MSN Messenger
Snootae
Grandmaster Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 969
Location: --->

PostPosted: Fri Jan 30, 2009 5:48 am    Post subject: Reply with quote

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
View user's profile Send private message
mathewthe2
Grandmaster Cheater
Reputation: 0

Joined: 28 Aug 2008
Posts: 562
Location: behind you... BOO!

PostPosted: Fri Jan 30, 2009 9:12 am    Post subject: Reply with quote

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.

_________________
This is what you get if you scan a noob game with my -Mth2- Scanner-
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites