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?
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?
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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,
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.
Last edited by High Octane; Jul 8th, 2005 at 09:04 PM.
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" ?
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.
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.
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.
YES YES YES dglienna, That's exactly wanted... so does it have to do once? can't I just restart it instead of closing form and run again? this code is perfect
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.
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?
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.
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.
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
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