Results 1 to 40 of 40

Thread: Random number if = True

  1. #1

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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:
    1. Dim Lucky As Byte
    2. Dim LuckyNum As Variant
    3.  
    4. If Lucky = 1 Then
    5.  
    6.  Lucky = 258 + 2 + 4 - 1
    7.  
    8. Else
    9.  
    10.  Lucky = 258 - 2 - 4 + 1
    11.  
    12. End If
    13.  
    14. LuckyNum = Lucky
    15. 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?
    High Octane

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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?

  3. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  4. #4
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    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:
    1. Option Explicit
    2. Dim OriginalNum As Integer, LuckyNum As Integer
    3. Dim Lucky As Byte
    4.  
    5. Private Sub chkLucky_Click()
    6.     'Use a checkbox to toggle true/false of Lucky.
    7.     Lucky = chkLucky.Value
    8.     'If Lucky is unchecked (0) reset to original number.
    9.     If Lucky = 0 Then LuckyNum = OriginalNum
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13.     OriginalNum = 258
    14.     LuckyNum = OriginalNum
    15.     Lucky = chkLucky.Value
    16. End Sub
    17.  
    18. Private Sub cmdCount_Click()
    19.     'Increase/decrease by 1 adjust according to your need.
    20.     LuckyNum = IIf(Lucky = 1, LuckyNum + 1, LuckyNum - 1)
    21.     Print Format(LuckyNum, "000")
    22. End Sub

    Have a good one!
    BK
    Last edited by Black__Knight; Jul 8th, 2005 at 07:07 PM.

  5. #5

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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?
    High Octane

  6. #6
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    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.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  8. #8
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    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

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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:
    1. Dim Lucky As Byte '<----- This will be fine if the numbers gonna be between 0-255
    2. Dim LuckyNum As Variant '<----- Never use variants. I would use Long in this case.
    3.  
    4. If Lucky = 1 Then
    5.  
    6.  [b]Lucky[/b] = 258 + 2 + 4 - 1 'Change this to LuckyNum
    7.  
    8. Else
    9.  
    10.  [b]Lucky[/b] = 258 - 2 - 4 + 1 'Change this to LuckyNum
    11.  
    12. End If
    13.  
    14. LuckyNum = Lucky 'Comment this line out
    15.  
    16. Print format(LuckyNum, "000" ' If true Print will show 263 otherwise it will go back to number 258

  10. #10

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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
    High Octane

  11. #11
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    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:
    VB Code:
    1. Randomize
    And this inside the timer sub:
    VB Code:
    1. 'set max and min
    2. rand = Int(Rnd * (Max - Min + 1)) + Min
    Or just use the link in my sig to get the random module I made.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  12. #12
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  13. #13
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Random number if = True

    Ok ignore what I said, I guess that wasn't what you were looking for.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  14. #14
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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?

  15. #15

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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
    High Octane

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  17. #17
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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?

  18. #18

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    Re: Random number if = True

    dglienna,

    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.
    High Octane

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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" ?

  20. #20

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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
    High Octane

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  22. #22

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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
    High Octane

  23. #23
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Random number if = True

    And you want it to sequence until a button is clicked?

  24. #24

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    Re: Random number if = True

    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.
    High Octane

  25. #25
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Random number if = True

    I'm hoping this is what you are asking.

    [EDIT] Reuploaded a fix to it.
    Attached Files Attached Files

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Random number if = True

    Try something like this:
    Attached Files Attached Files

  27. #27
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  28. #28
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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:
    1. If Counter = 4 Then
    2.                    
    3.                     Last_Seconds = Get_Elapsed_Seconds
    4.                    
    5.                     Counter = 0
    6.                    
    7.                 End If
    8.                    
    9.             Else
    10.                
    11.                 Lucky = 258 'It's equivalant to this: 258 - 2 - 4 + 1
    12.                
    13.             End If

    to this:

    VB Code:
    1. If Counter = 4 Then
    2.                    
    3.                     Last_Seconds = Get_Elapsed_Seconds
    4.                    
    5.                     Counter = 0
    6.                    
    7.                 End If
    8.                    
    9.             Else
    10.                
    11.                 [b]Last_Seconds = Get_Elapsed_Seconds[/b]
    12.                
    13.                 Lucky = 258 'It's equivalant to this: 258 - 2 - 4 + 1
    14.                
    15.             End If

    Forgot to add that. Im gonna reupload my project with this change.

  29. #29

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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.
    High Octane

  30. #30

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    Re: Random number if = True

    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
    High Octane

  31. #31
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Random number if = True

    What about mine?

  32. #32

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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.
    High Octane

  33. #33

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    Re: Random number if = True

    Oh YOU DID great dglienna check out on post 30....
    High Octane

  34. #34
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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?

  35. #35

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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 = )
    High Octane

  36. #36
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  37. #37

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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.
    High Octane

  38. #38
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  39. #39

    Thread Starter
    Hyperactive Member High Octane's Avatar
    Join Date
    Aug 2003
    Location
    Texas
    Posts
    290

    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
    High Octane

  40. #40
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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:
    1. Private Sub Form_Load()
    2.   dim a as integer
    3.   randomize
    4.   for a=0 to 3
    5.     num(a) = int(rnd * 300 ) + 1 ' lowest # is 1.  add as many as you want
    6.   next a
    7.    Label1.Caption = ""
    8.   Command1.Caption = "Pick"
    9.   Timer1.Interval = 500
    10.   Timer1.Enabled = False
    11.   tmr = False
    12. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width