-
Random number if = True
Guys,
This question may sound complicated... And I was hoping it's possible to do it. what I wanted is to have random number to change when variable is true or 1, then change the random number can change back to original when variable is false or 0 for example here's snippet:
VB Code:
Dim Lucky As Byte
Dim LuckyNum As Variant
If Lucky = 1 Then
Lucky = 258 + 2 + 4 - 1
Else
Lucky = 258 - 2 - 4 + 1
End If
LuckyNum = Lucky
Print format(LuckyNum, "000" ' If true Print will show 263 otherwise it will go back to number 258
I have tried this similar code using command1 button as if I pressed to be true, and I end up with overflow and could not understand why, Also, I was expecting number to increase sequently in delay time for instance when its true or 1, the display will show 258,260,264 then 263. When its false or 0 it will countdown sequently 264,260,258 and etc. Perhaps, I have done wrong or anything. done search on this site seeing if anyone ever encountered or trying these methods... and end up with no luck. Can anyone help?
-
Re: Random number if = True
Do you want the number to change when you click the button, depending on the value of the flag? It looks like you want to add 2 if its true, then subtract 2 when it's false, or adds 2+4 when it's true, and then subtracts 2-4 when it's false. Is that what you want? Is it only going to cycle between 3 clicks?
In other words, if you've already added 3 times, are you going to add again?
-
Re: Random number if = True
You need to change your data types. Byte is only limited to 0 - 255 and you're working with numbers bigger than that, like 263, 258, etc. And you should never use the Variant data type. It's the number one cause of slowing your program and it takes longer to compute I believe too.
-
Re: Random number if = True
Hi,
I am not sure of what you want but this example will increment an original value by 1 as the checkbox is checked and will reset back to the original number and start decreasing by 1 when it is cleared.
VB Code:
Option Explicit
Dim OriginalNum As Integer, LuckyNum As Integer
Dim Lucky As Byte
Private Sub chkLucky_Click()
'Use a checkbox to toggle true/false of Lucky.
Lucky = chkLucky.Value
'If Lucky is unchecked (0) reset to original number.
If Lucky = 0 Then LuckyNum = OriginalNum
End Sub
Private Sub Form_Load()
OriginalNum = 258
LuckyNum = OriginalNum
Lucky = chkLucky.Value
End Sub
Private Sub cmdCount_Click()
'Increase/decrease by 1 adjust according to your need.
LuckyNum = IIf(Lucky = 1, LuckyNum + 1, LuckyNum - 1)
Print Format(LuckyNum, "000")
End Sub
Have a good one!
BK
-
Re: Random number if = True
Yeah, something like that... but may be a little complicated
Hmm let me try to draw in text:
258 (default number which appears on form at first run)
if (Variable) = 1 Then => Result 288
if (Variable) = 0 Then => 258
Alternatively, I was hoping, when its true, number increase and then it fluctuates ie. 288, 290,287,289, (flucts at avg of 288 to 291) and etc, then when its states at 0 then it goes back to 258 and set it forever until 1 is called upon again.
Hope that clears up?
-
Re: Random number if = True
Try setting a break point on the first line of code (using F9). Then use F8 to step into each line of code. Hove your mouse over expressions that you want to know the value of. Its a good technique to use especially in situations like yours :). You can watch exactly what the program is doing and if you learn it now it will help you a lot in the future.
-
Re: Random number if = True
Again, Lucky should be declared something other than Byte. It needs to be declared Integer or Long to prevent overflowing. Plus he's working with numbers larger than 255. And you should never use IIF(). We proved this a while ago in one of these threads, but it is the slowest If statement of them all. Very slow infact. Although it doesnt matter in his program or yours, it should never be used in something like Loops or Timers, especially when making a game.
-
Re: Random number if = True
Hi,
Jacob:
Lucky can be byte since it is a checkbox value 0-2. <g> LuckyNum is an integer. -32767...32767 (in my example at least) <g>
Variants are intrinsically slower but to the point of NEVER using them LOL.
Have a good one!
BK
-
Re: Random number if = True
Quote:
Originally Posted by Black__Knight
Hi,
Jacob:
Lucky can be byte since it is a checkbox value 0-2. <g> LuckyNum is an integer. -32767...32767 (in my example at least) <g>
Variants are intrinsically slower but to the point of NEVER using them LOL.
Have a good one!
BK
Not according to his code, unless he has many typos going. If I were him, I change the variables in bold to LuckyNum:
VB Code:
Dim Lucky As Byte '<----- This will be fine if the numbers gonna be between 0-255
Dim LuckyNum As Variant '<----- Never use variants. I would use Long in this case.
If Lucky = 1 Then
[b]Lucky[/b] = 258 + 2 + 4 - 1 'Change this to LuckyNum
Else
[b]Lucky[/b] = 258 - 2 - 4 + 1 'Change this to LuckyNum
End If
LuckyNum = Lucky 'Comment this line out
Print format(LuckyNum, "000" ' If true Print will show 263 otherwise it will go back to number 258
-
Re: Random number if = True
Black__Knight, I liked your code... look like it work good... But its like a countdown from 258 and minus...
Jacob Roman
LuckyNum = Lucky This string retrieve information from lucky so it can print on the form like this below
Print format(LuckyNum, "000")
It doesn't require to do this, I could simply add Lucky inside the parenthesis instead of LuckyNum. But I did that to avoid confusion, just having habit of organizing codes.
Guys, sorry for so many confusions.... here what I am going to ask... is it possible to get number to fluxuating when the variable is true based on delay time or timeinterval then it stops when variable is not true?
for example:
if lucky = 0 then 258 labeled on form (stays there no changes)
if lucky = 1 then 266 (.5 ms) 271 (.5 ms) 264 (.5ms) and repeats....
if lucky = 0 (again) 258 labeled again and (stays there no change)
of course with cls and auto redraw so it wont creates list downs, but overlapiing.
Let me know
-
Re: Random number if = True
I'm not sure exactly what you are asking, but if you just need a random number put this inside the Form_Load sub:And this inside the timer sub:
VB Code:
'set max and min
rand = Int(Rnd * (Max - Min + 1)) + Min
Or just use the link in my sig to get the random module I made.
-
Re: Random number if = True
Yes it's possible. Matter of fact, I can show you how to do it using real time very accurately, since you are returning a result every 5 milliseconds, but I think it may confuse you since I'll be using some API's you never seen before. Do you want me to show you anyways?
And yes, that's how you make numbers truly random in the Rnd function. You put Randomize in there as the first line of code in your sub. I heard that the only place you really need to put it in though is the Form_Load and you never have to call it again, and Rnd will always be random.
-
Re: Random number if = True
Ok ignore what I said, I guess that wasn't what you were looking for.
-
Re: Random number if = True
Well the thread title seems as though that is what he's looking for. :)
Is that what you are trying to do, High Octane?
-
Re: Random number if = True
Monkey, I am not really sure I know how to implement your example in my code... and I am not really sure I understand your code example on other end "linked" site you prevously did yesterday. But if you really think it works I can give it a try
Jacob roman, Show me what you got... I need to learn how to use API now :)
-
Re: Random number if = True
You want the numbers to change based on the range between Lucky and LuckyNum? I haven't seen your answers to any of my questions, that is why I haven't written any code. I have code to count down and count up in a timer (which is what JR is trying to show you)
You don't really need a random number, but to generate +/- numbers twice per second that grow as a result of time?
EDIT: Explain what the program is, and what you want it to do, and we'll be able to help you do what YOU want it to do instead of us trying to guess.
-
Re: Random number if = True
Quote:
Originally Posted by High Octane
Jacob roman, Show me what you got... I need to learn how to use API now :)
Before I do, I just wanna know if you are wanting truly random numbers or if you are emulating random numbers.
Knowing so will help me write out the program you are wanting. ;)
[EDIT] Or are you just wanting it to count up/down? :ehh:
-
Re: Random number if = True
dglienna,
Quote:
You want the numbers to change based on the range between Lucky and LuckyNum?
Lucky and LuckyNum is basically same variable. I used LuckyNum so it can print out the numbers on the form... Actually I don't really need to add luckynum in the code its just my habit of organizing codes.
Quote:
I haven't seen your answers to any of my questions, that is why I haven't written any code. I have code to count down and count up in a timer (which is what JR is trying to show you)
I apologize if I didn't answer your question, I didnt catch it to many responses probably got me overlook to your question(s). Could you show me your example? maybe you and JR can compare it and give out the best need.
Quote:
You don't really need a random number, but to generate +/- numbers twice per second that grow as a result of time?
No, I just want a number given, one that already write in program then when time is their the number displays it. (Will explain you clearly below)
Jacob Roman,
Quote:
Before I do, I just wanna know if you are wanting truly random numbers or if you are emulating random numbers.
Knowing so will help me write out the program you are wanting.Or are you just wanting it to count up/down?
Just Emulating given numbers... i.e. 256, 275, 260. Numbers doesn't really have to be in perfect order.
Jacob Roman and dglienna,
Here what I wanted it to do... Given 4 numbers: 270, 266, 274, and 281
application running =>
*If (variable) = 0 then display 258 <=locked like tatoo
*If (Variable)= 1 Then
--->First display 270 (stays there for .5ms)then
--->Second display 266 (stays there for .5ms) then
--->Third display 274 (stays there for .5ms) then
--->Fourth display 281 (stays there for .5ms) then
---:> Loop it going back to start then
--->First display 270 (stays there for .5ms)then
--->Second display 266 (stays there for .5ms) then
--->Third display 274 (stays there for .5ms) then
--->Fourth display 281 (stays there for .5ms) then
Until variable states 0 again the displays will show what is last shown ie "281" or something.
Hope that clears up.
-
Re: Random number if = True
So you want to input 4 numbers and then sequence through them?
Like having 4 items in a listbox, and going from 1 to 2 to 3 to 4 and then back to 1? You want to input them in one textbox, separated by a "," like text1.text = "220,242,255,300" ?
-
Re: Random number if = True
Yup thats what I wanted except for one.. I didn't want to use textboxes, I just wanted it to print on form using print command.
Of course use cls command to clear out so it can overlay previous numbers
-
Re: Random number if = True
Where are the numbers coming from? Aren't your users going to input them from somewhere? What are you trying to do? That may help.
-
Re: Random number if = True
maybe setting const or something such as
const1= 270
const2= 266
const3= 274
const4= 281
or
Dim A As Long
Dim B As Long
Dim C As Long
Dim D As Long
A= 270
B= 266
C= 274
D= 281
-
Re: Random number if = True
And you want it to sequence until a button is clicked?
-
Re: Random number if = True
Quote:
And you want it to sequence until a button is clicked?
Yeah like a switch.... when run application... its off... then when click on button it starts sequences then click again on same button or another button it stops sequences.
-
1 Attachment(s)
Re: Random number if = True
I'm hoping this is what you are asking. :)
[EDIT] Reuploaded a fix to it. :bigyello:
-
1 Attachment(s)
Re: Random number if = True
-
Re: Random number if = True
JR, I tried that. Did you? Not feeling lucky, I got 258. ONLY, with no way to stop it! At least when I checked the box, I got random numbers, until I KILLED the program.
-
Re: Random number if = True
Thats what he wanted. He wanted it locked onto 258 when off. And change this in the Main_Module:
VB Code:
If Counter = 4 Then
Last_Seconds = Get_Elapsed_Seconds
Counter = 0
End If
Else
Lucky = 258 'It's equivalant to this: 258 - 2 - 4 + 1
End If
to this:
VB Code:
If Counter = 4 Then
Last_Seconds = Get_Elapsed_Seconds
Counter = 0
End If
Else
[b]Last_Seconds = Get_Elapsed_Seconds[/b]
Lucky = 258 'It's equivalant to this: 258 - 2 - 4 + 1
End If
Forgot to add that. Im gonna reupload my project with this change.
-
Re: Random number if = True
JR Thanks for your effort.. the code you wrote did not stop, and this put my computer in blue screen of death after I killed it. This is my first time I ever saw it after adding 1Gb memory about a year ago. So seem like too much 258 is bundled up.
Well, frankly, this isn't exactly what I wanted... I didn't want it to be bundled up... I just want it to overwrite the previous indicated numbers on same spots... look at previous post #18 -24.
But I do appreciate your efforts.
-
Re: Random number if = True
YES YES YES dglienna, That's exactly wanted... :thumb: so does it have to do once? can't I just restart it instead of closing form and run again? this code is perfect
-
Re: Random number if = True
-
Re: Random number if = True
One more thing, I see you use label instead of print command. I actually wanted to use print command. The reason why using print command is because it doesn't flick when sitting behind autoredraw picture... label does.... unless you know how to stop label from flicker when it sit behind autoredraw pictures.
-
Re: Random number if = True
Oh YOU DID great dglienna check out on post 30....
-
Re: Random number if = True
Well, in the timer event, you can change that to whatever you want. I wasn't going to print on the form with the button on it. It was more of an example of how to do what I thought you wanted. Is that it?
-
Re: Random number if = True
Yeah this is what I wanted... you did great work...in the meantime THANK YOU SO MUCH for helping I truly appreciate that = )
-
Re: Random number if = True
Glad I guess right. At first, I thought you wanted to get the difference between 264 and 272 (8) divided by 2 (4) and add or subtract 4.
It helps to just post what you have, and what you want. We can prolly see the easiest way to do it, while you may be able to see how you want it, but not how the computer can do it.
-
Re: Random number if = True
Well I did at first... (mathematic computing) but I realise that would put too much complications.. so going other way with your code, it does better. I guess I don't demand mathematic methods but have number to display randomly.
-
Re: Random number if = True
You can still pick random numbers, if you want, instead of hard coding them in.
This way will produce the same numbers every time.
-
Re: Random number if = True
I would stick with it :) I just played around code adding stop and start... I was able to reset it without getting it stop at once and I like it very much :thumb:
-
Re: Random number if = True
This will pick 4 numbers less than 300. It adds 1 so that the lowest isn't 0, but if you want you can change the 1 to 100 so that the lowest is 100, or change the 300 to 400 if you want 299 to be the highest.
VB Code:
Private Sub Form_Load()
dim a as integer
randomize
for a=0 to 3
num(a) = int(rnd * 300 ) + 1 ' lowest # is 1. add as many as you want
next a
Label1.Caption = ""
Command1.Caption = "Pick"
Timer1.Interval = 500
Timer1.Enabled = False
tmr = False
End Sub