QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: badger on October 26, 2020, 04:22:55 pm

Title: question about PEEK and POKE - hello need to know what this is doing
Post by: badger on October 26, 2020, 04:22:55 pm
Hello

I know nothing of peek and poke could some one tell me what this is doing

Badger

Code: QB64: [Select]
  1. FOR il = 1 TO 59
  2.     POKE 5000 + il, il
  3. FOR il = 1 TO 59
  4.     PRINT PEEK(5000 + il);
  5.  
Title: Re: hello need to know what this is doing
Post by: badger on October 26, 2020, 04:27:59 pm
Hello

This is the code the first post is from .. looking over it again i know what it does but i need to know how and what the peek and poke statements are doing.

Badger

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. FOR l = 1 TO 59
  5.     POKE 5000 + l, l
  6. FOR l = 1 TO 59
  7.     PRINT PEEK(5000 + l);
  8. PRINT "Now drawing powerball numbers..."
  9. ct = 1
  10. here:
  11. x = INT(RND * 59) + 1
  12. IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  13. GOTO here
  14. there:
  15.  
  16. ct = ct + 1
  17.  
  18. IF ct = 6 THEN GOTO done ELSE GOTO here
  19. done:
  20. PRINT "Here are your 5 Powerball numbers:"
  21. FOR ct = 1 TO 5
  22.     PRINT r(ct);
  23. PRINT "The red Powerball number is:";
  24. PRINT INT(RND * 35) + 1
  25. PRINT "time started  : "; a$
  26. PRINT "time completed: "; TIME$
  27.  
  28.  
  29.  
Title: Re: hello need to know what this is doing
Post by: SpriggsySpriggs on October 26, 2020, 04:28:12 pm
Well, I don't know the addresses it is referring to but PEEK is supposed to read a memory address on the PC and show the value stored there. POKE is used to write a value to a memory address. The old ways don't work too well on modern QB64 (since they overflow a lot) and I have a library here https://www.qb64.org/forum/index.php?topic=3134.msg124049#msg124049 (https://www.qb64.org/forum/index.php?topic=3134.msg124049#msg124049) that is able to do the task on Windows 64 and 32 bit.
Title: Re: hello need to know what this is doing
Post by: Kernelpanic on October 26, 2020, 04:44:49 pm
Poke is a Adress in RAM:  Poke 5000, 33
Means, Adress: 5000, Value: 33
Peek is:  x = Peek(Adress)

Code: QB64: [Select]
  1.  
  2. POKE 5000, 33
  3.  
  4. x = PEEK(5000)
  5.  

It is like pointer in C.
Title: Re: hello need to know what this is doing
Post by: badger on October 26, 2020, 04:50:18 pm
SpriggsySpriggs


what do these const values mean if i may ask ...

Badger

I dont mean to ask for a lesson but that is what i might be asking LOL



Code: QB64: [Select]
  1.  
  2. CONST PROCESS_VM_READ = &H0010
  3. CONST PROCESS_QUERY_INFORMATION = &H0400
  4. CONST PROCESS_VM_WRITE = &H0020
  5. CONST PROCESS_VM_OPERATION = &H0008
  6. CONST STANDARD_RIGHTS_REQUIRED = &H000F0000
  7. CONST SYNCHRONIZE = &H00100000
  8. CONST PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED OR SYNCHRONIZE OR &HFFFF
  9.  
  10. CONST TH32CS_INHERIT = &H80000000
  11. CONST TH32CS_SNAPHEAPLIST = &H00000001
  12. CONST TH32CS_SNAPMODULE = &H00000008
  13. CONST TH32CS_SNAPMODULE32 = &H00000010
  14. CONST TH32CS_SNAPPROCESS = &H00000002
  15. CONST TH32CS_SNAPTHREAD = &H00000004
  16. CONST TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST OR TH32CS_SNAPMODULE OR TH32CS_SNAPPROCESS OR TH32CS_SNAPTHREAD
  17.  
Title: Re: hello need to know what this is doing
Post by: badger on October 26, 2020, 05:02:08 pm
Hello

I think i know what the lottery program is doing with the peeks and pokes

he first pokes the value 1 to 50. In address 5000 1 to 59

then generates a random number by x= int(rnd*59)+1
then in the next line he compares the value of x to that value by using peek to make sure there is no zero or duplicates in his number

Correct ????

Badger
Title: Re: hello need to know what this is doing
Post by: SpriggsySpriggs on October 26, 2020, 05:07:46 pm
what do these const values mean if i may ask ...

@badger The const values are just what is required for running the specific commands so you don't have to memorize those hexadecimal numbers. They correspond to the declarations from the WinAPI. You don't really need to know what they do since they're only used by the functions I made. However, the first block is used for gaining rights to a process. The second block is used for gaining rights for finding the process.

he first pokes the value 1 to 50. In address 5000 1 to 59

Yes, it looks like he is poking those addresses but I was saying that I don't know what those addresses refer to. I was hesitant to run it on my PC since I wasn't sure what it was going to do. I know it is supposed to generate PowerBall numbers. I ran it on Linux 64 bit as well as Windows 10 in 32 and 64 bit. It worked. Everything seems straightforward and you seem to understand what is happening.
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SpriggsySpriggs on October 27, 2020, 01:37:18 am
@badger Here is some code I made using the WinAPI code that I mentioned and you've looked at. This does the Powerball draw using that library :) No guarantee that it is exactly the same result or that it will be stable. I got numbers (sometimes) when I ran it. Don't take this as me telling you to use this instead. Your code you posted at first works better for that sort of thing. I was trying to see if I could do it as well. My library is better suited for the stuff like in the example from my post about Peeping Tom. It's good for editing another program's memory or doing peeks and pokes in your program.

Code: QB64: [Select]
  1. $IF 64BIT THEN
  2.     TYPE PROCESSENTRY32
  3.         dwSize AS LONG
  4.         cntUsage AS LONG
  5.         th32ProcessID AS _INTEGER64
  6.         th32DefaultHeapID AS _UNSIGNED _INTEGER64
  7.         th32ModuleID AS LONG
  8.         cntThreads AS LONG
  9.         th32ParentProcessID AS LONG
  10.         pcPriClassBase AS LONG
  11.         dwFlags AS LONG
  12.         szExeFile AS STRING * 260
  13.     END TYPE
  14.     TYPE PROCESSENTRY32
  15.     dwSize AS LONG
  16.     cntUsage AS LONG
  17.     th32ProcessID AS LONG
  18.     th32DefaultHeapID AS _UNSIGNED LONG
  19.     th32ModuleID AS LONG
  20.     cntThreads AS LONG
  21.     th32ParentProcessID AS LONG
  22.     pcPriClassBase AS LONG
  23.     dwFlags AS LONG
  24.     szExeFile AS STRING * 260
  25.     END TYPE
  26.  
  27. CONST PROCESS_VM_READ = &H0010
  28. CONST PROCESS_QUERY_INFORMATION = &H0400
  29. CONST PROCESS_VM_WRITE = &H0020
  30. CONST PROCESS_VM_OPERATION = &H0008
  31. CONST STANDARD_RIGHTS_REQUIRED = &H000F0000
  32. CONST SYNCHRONIZE = &H00100000
  33. CONST PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED OR SYNCHRONIZE OR &HFFFF
  34.  
  35. CONST TH32CS_INHERIT = &H80000000
  36. CONST TH32CS_SNAPHEAPLIST = &H00000001
  37. CONST TH32CS_SNAPMODULE = &H00000008
  38. CONST TH32CS_SNAPMODULE32 = &H00000010
  39. CONST TH32CS_SNAPPROCESS = &H00000002
  40. CONST TH32CS_SNAPTHREAD = &H00000004
  41. CONST TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST OR TH32CS_SNAPMODULE OR TH32CS_SNAPPROCESS OR TH32CS_SNAPTHREAD
  42.  
  43. CONST TRUE = -1
  44. CONST FALSE = 0
  45.  
  46.     FUNCTION CreateToolhelp32Snapshot& (BYVAL dwFlags AS LONG, BYVAL th32ProcessID AS LONG)
  47.     FUNCTION Process32First%% (BYVAL hSnapshot AS LONG, BYVAL lppe AS _OFFSET)
  48.     FUNCTION Process32Next%% (BYVAL hSnapshot AS LONG, BYVAL lppe AS _OFFSET)
  49.     FUNCTION OpenProcess& (BYVAL dwDesiredAccess AS LONG, BYVAL bInheritHandle AS _BYTE, BYVAL dwProcessId AS LONG)
  50.     FUNCTION ReadProcessMemory%% (BYVAL hProcess AS LONG, BYVAL lpBaseAddress AS _OFFSET, BYVAL lpBuffer AS _OFFSET, BYVAL nSize AS LONG, BYVAL lpNumberOfBytesRead AS _OFFSET)
  51.     FUNCTION WriteProcessMemory%% (BYVAL hProcess AS LONG, BYVAL lpBaseAddress AS _OFFSET, BYVAL lpBuffer AS _OFFSET, BYVAL nSize AS LONG, BYVAL lpNumberOfBytesWritten AS _OFFSET)
  52.     FUNCTION CloseHandle%% (BYVAL hObject AS LONG)
  53.  
  54.     FUNCTION strlen& (BYVAL ptr AS _UNSIGNED _OFFSET)
  55.  
  56. exe = MID$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\") + 1)
  57.  
  58. a = TIME$
  59. PRINT "Drawing..."
  60. DIM pokval AS INTEGER
  61. pokval = 5000
  62. FOR l = 1 TO 59
  63.     pok = pokeint(exe, _OFFSET(pokval) + l, l)
  64. FOR l = 1 TO 59
  65.     pok = peekint(exe, _OFFSET(pokval) + l)
  66. PRINT "Now drawing powerball numbers..."
  67. ct = 1
  68.     x = INT(RND * 59) + 1
  69.     IF peekint(exe, _OFFSET(pokval) + x) <> 0 THEN
  70.         REDIM _PRESERVE r(ct) AS INTEGER
  71.         r(ct) = x
  72.     END IF
  73.     pok = pokeint(exe, _OFFSET(pokval) + x, 0)
  74.     ct = ct + 1
  75.  
  76.     IF ct = 6 THEN EXIT DO
  77.  
  78. PRINT "Here are your 5 Powerball numbers:"
  79. FOR ct = 1 TO 5
  80.     PRINT r(ct);
  81. PRINT "The red Powerball number is:";
  82. PRINT INT(RND * 35) + 1
  83. PRINT "time started  : "; a
  84. PRINT "time completed: "; TIME$
 [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 10:36:13 am
Why PEEK and POKE for random numbers?
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SMcNeill on October 27, 2020, 02:09:34 pm
Why PEEK and POKE for random numbers?

It's a very obfuscating way to simply make certain that the values don't appear twice in the same drawing.  After looking over the original code, I went in and made what I thought was some simple Steve Edits(tm)  to bring the code up to more modern programming practices:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. REM Which Steve finds to be a very goofy way to do Powerball numbers...
  3. REM So this is the modified version, which doesn't use GOTO, or obsolete PEEK and POKE commands.
  4.  
  5.  
  6.  
  7. a$ = TIME$
  8. PRINT "Drawing..."
  9.  
  10. 'FOR l = 1 TO 59
  11. 'POKE 5000 + l, l
  12. 'NEXT
  13. b$ = "11111111111111111111111111111111111111111111111111111111111" 'Steve addition of a 59 length string of all 1's.
  14.  
  15. 'SLEEP 3                             'Why a sleep statement here?  If we're going to time program execution, we don't need it to slow things down
  16. 'FOR l = 1 TO 59                     'For that much, why are we even bothering with these PRINT statements at all?  The CLS below erases them from the screen too fast for us to even register them with our human eyesight...
  17. '    PRINT PEEK(5000 + l);
  18. '    PRINT l;                        'Even if we do print our values to the screen, WTH do we need to PEEK the numbers from 1 to 59?  We just put them in memory with POKE in perfect order.  Why not just print them and be done with it?
  19. 'NEXT
  20. 'PRINT "Now drawing powerball numbers..."
  21. 'ct = 1
  22. 'here:
  23. FOR ct = 1 TO 6 '                      We want 6 numbers , so a FOR loop makes sense here, rather than a GOTO label
  24.     DO '                               A simple DO loop, to get an unique X. If we draw the same number twice, simply redraw until it's not an used number
  25.         x = INT(RND * 59) + 1
  26.         'IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  27.         'GOTO here
  28.     LOOP UNTIL MID$(b$, x, 1) = "1" '  End of the Steve added DO loop
  29.     r(ct) = x: MID$(b$, x, 1) = "0" '  Basically the same as the code after the THEN which I commented out above, but using a simple string rather than PEEK and POKE to check for used numbers.
  30. 'there: '                              All needlessly complicated GOTO labels and junk, which simply obfuscates the code.  These were all commented out and removed by Steve
  31.  
  32. '   ct = ct + 1
  33.  
  34. '  IF ct = 6 THEN GOTO done ELSE GOTO here
  35. ' done:
  36. 'CLS '                                 No need for a CLS here, or else there's no need for the printing above, as it'll simply dash off the screen too quick to read.
  37. PRINT "Here are your 5 Powerball numbers:"
  38. FOR ct = 1 TO 5
  39.     PRINT r(ct);
  40. PRINT "The red Powerball number is:";
  41. PRINT INT(RND * 35) + 1
  42. PRINT "time started  : "; a$
  43. PRINT "time completed: "; TIME$
  44.  



And, to compare the two, without all the comments in the way, here's the original again:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. FOR l = 1 TO 59
  5.     POKE 5000 + l, l
  6. FOR l = 1 TO 59
  7.     PRINT PEEK(5000 + l);
  8. PRINT "Now drawing powerball numbers..."
  9. ct = 1
  10. here:
  11. x = INT(RND * 59) + 1
  12. IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  13. GOTO here
  14. there:
  15.  
  16. ct = ct + 1
  17.  
  18. IF ct = 6 THEN GOTO done ELSE GOTO here
  19. done:
  20. PRINT "Here are your 5 Powerball numbers:"
  21. FOR ct = 1 TO 5
  22.     PRINT r(ct);
  23. PRINT "The red Powerball number is:";
  24. PRINT INT(RND * 35) + 1
  25. PRINT "time started  : "; a$
  26. PRINT "time completed: "; TIME$

And here's the altered version, without the extra junk left in it:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Steve.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. b$ = "11111111111111111111111111111111111111111111111111111111111"
  5. FOR ct = 1 TO 5
  6.     DO
  7.         x = INT(RND * 59) + 1
  8.     LOOP UNTIL MID$(b$, x, 1) = "1"
  9.     r(ct) = x: MID$(b$, x, 1) = "0"
  10. PRINT "Here are your 5 Powerball numbers:"
  11. FOR ct = 1 TO 5
  12.     PRINT r(ct);
  13. PRINT "The red Powerball number is:";
  14. PRINT INT(RND * 35) + 1
  15. PRINT "time started  : "; a$
  16. PRINT "time completed: "; TIME$

From a 34 line program with PEEKs and POKEs, labels and GOTOs, to a simple little 21 line program.  This goes from something which has new (or modern) coders scratching their heads and saying, "What the heck does this do?", (such as in the original post), to becoming something simple enough for anyone to basically glance at and figure out what we're doing.

A lot of these old commands are in the language just for backwards compatibility reasons.  They're not something which a respectable, decent modern-day programmer needs in their toolbox.  PEEK and POKE are two commands best simply forgotten about, unless you're having to maintain/convert legacy code from the 90's.  And GOTO.... GOTO is still a need tool for the modern programmer, but it shouldn't be the first tool of choice, when simple FOR-NEXT, DO-LOOP, and WHILE-WEND statements can replace it easily in most cases and make the code much more readable and easier to figure out.
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 05:58:04 pm
21 lines what are you doing playing around like that?  ;-))
Code: QB64: [Select]
  1. _TITLE "Powerball 1-69 redball 1 to 26" 'b+ 2020-10-27
  2. ' ref: https://www.google.com/search?client=opera&q=powerball+number+range&sourceid=opera&ie=UTF-8&oe=UTF-8
  3. t# = TIMER(.01)
  4. DEFINT A-Z
  5. DIM pb(1 TO 69)
  6. FOR i = 1 TO 69
  7.     pb(i) = i
  8. FOR i = 69 TO 2 STEP -1
  9.     SWAP pb(i), pb(INT(RND * i) + 1)
  10. FOR i = 20 TO 24
  11.     PRINT pb(i);
  12. PRINT "redball:"; INT(RND * 26) + 1; "   time:"; TIMER(.01) - t#
  13.  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: badger on October 27, 2020, 06:22:36 pm
Hello

I did not mean to start a competition rotf but it was fun to watch. I was going to rewrite it using arrays and such on the same principal. If you dont need to peek and poke it is a good idea to not. but i just wanted a little more understanding of what he was doing. both programs are really cute.

Badger
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 06:30:21 pm
ha yeah I am still fixing mine, found a glaring error.
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: badger on October 27, 2020, 06:36:38 pm
Hey

Repost the code if you would ok .. want to see if i can pick out the differences

Badger
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: badger on October 27, 2020, 06:50:04 pm
SpriggsySprigs

i copied your code but get illegal string number conversion on this code. If i rem it out it just does it on the next instance.
 pok = pokeint(exe, _OFFSET(pokval) + l, l)

Badger
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 06:57:00 pm
Hey

Repost the code if you would ok .. want to see if i can pick out the differences

Badger

It is the difference between post at 6:07 and 6:27? the last edit time about 20 min difference. Did you know you can read earlier posts?
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: badger on October 27, 2020, 07:06:58 pm
Hey

Why did you just pic numbers in the pb array 20 to 24 to be the numbers drawn.

Badger
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 07:12:28 pm
Hey

Why did you just pic numbers in the pb array 20 to 24 to be the numbers drawn.

Badger

I picked 5 straight numbers at random it could be any 5 of the shuffled set of numbers. 1 to 5, 10 to 14,
or the 2, 3, 5, 7, 11th ;-))
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SMcNeill on October 27, 2020, 07:31:19 pm
21 lines what are you doing playing around like that?  ;-))
Code: QB64: [Select]
  1. _TITLE "Powerball 1-69 redball 1 to 26" 'b+ 2020-10-27
  2. ' ref: https://www.google.com/search?client=opera&q=powerball+number+range&sourceid=opera&ie=UTF-8&oe=UTF-8
  3. t# = TIMER(.01)
  4. DEFINT A-Z
  5. DIM pb(1 TO 69)
  6. FOR i = 1 TO 69
  7.     pb(i) = i
  8. FOR i = 69 TO 2 STEP -1
  9.     SWAP pb(i), pb(INT(RND * i) + 1)
  10. FOR i = 20 TO 24
  11.     PRINT pb(i);
  12. PRINT "redball:"; INT(RND * 26) + 1; "   time:"; TIMER(.01) - t#
  13.  

16 lines?   PAH!!  I can easily go half that!  :P

Code: QB64: [Select]
  1. FOR ct = 1 TO 5
  2.     DO
  3.         x = INT(RND * TIMER(0.01) * 100) MOD 59 + 1
  4.     LOOP UNTIL SCREEN(2, x) = 32
  5.     LOCATE 1, 1
  6.     PRINT "Here are your Powerball numbers:"; TAB(46 - (ct - 1) * 3); x; CHR$(10); TAB(x); CHR$(0) 'the second line holds invisible spaces (chr$(0)) for used numbers
  7. PRINT "The red Powerball number is:"; INT(RND * 35) + 1



Hello

I did not mean to start a competition rotf but it was fun to watch. I was going to rewrite it using arrays and such on the same principal. If you dont need to peek and poke it is a good idea to not. but i just wanted a little more understanding of what he was doing. both programs are really cute.

Badger

It's all fun and games.  bplus and I enjoy having fun pushing each other with different little challenges, with "shortest code" being one of the things we often compare. 

I've got to admit though, sometimes, the code one of us ends up posting just absolutely blows my mind with what all can be done with just a minimal number of lines of coding.  It's not always the prettiest, but it's sometimes astonishing to see a 100 line program can trim down to 10 lines, and still not look fundamentally different.

Warning though:  After a few times of batting the numbers back and forth, always giving the other fellow a smaller target to shoot for, the code can often become quite obfuscated with obscure keywords, inane coding logic, or mystical voodoo which isn't always for the faint of heart to try and decipher.  Shortest code possible isn't always the best code possible, but it is a nice exercise in logic for folks to play around with once they're comfortable with the language and want to challenge themselves to something different.

@bplus: 8 lines to you.  Can you come up with something shorter, that's guaranteed not to produce duplicate values?  ;D
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 07:41:46 pm
Quote
16 lines?   PAH!!  I can easily go half that!  :P

;) there ya go!

I learn allot from this!
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 07:57:27 pm
Quote
@bplus: 8 lines to you.  Can you come up with something shorter, that's guaranteed not to produce duplicate values?  ;D

Why yes, yes I can! ;-))
Code: QB64: [Select]
  1. RANDOMIZE TIMER ' add this if you need to see a different one, I did
  2. PRINT "Powerball:"; (RND * 5) \ 1 + 1; (RND * 5) \ 1 + 7; (RND * 5) \ 1 + 13; (RND * 5) \ 1 + 19; (RND * 5) \ 1 + 25; "  Red Powerball:"; INT(RND * 26) + 1
  3.  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SMcNeill on October 27, 2020, 08:06:50 pm
Why yes, yes I can! ;-))
Code: QB64: [Select]
  1. RANDOMIZE TIMER ' add this if you need to see a different one, I did
  2. PRINT "Powerball:"; (RND * 5) \ 1 + 1; (RND * 5) \ 1 + 7; (RND * 5) \ 1 + 13; (RND * 5) \ 1 + 19; (RND * 5) \ 1 + 25; "  Red Powerball:"; INT(RND * 26) + 1
  3.  

Now that's not a true powerball generator.  You're generating 5 blocks of numbers, not 5 numbers from one block of values.  That's got to do something terrible to the odds of winning in the program!  (And sounds like a good math problem for STx to have fun and solve for us.  I'd have to go dust off the old math books and do a lot more research than I'm interested in to figure that answer out.)  :P

I'm playing ump and calling "Foul Balls!" 
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 08:10:18 pm
Quote
I'm playing ump and calling "Foul Balls!"

Hey desperate times call for desperate measures. :-)) How else am I going to beat 8 lines?

You're just jealous ;-))
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SMcNeill on October 27, 2020, 08:29:31 pm
Hey desperate times call for desperate measures. :-)) How else am I going to beat 8 lines?

You're just jealous ;-))

Here's one way -- I dropped it down to only 7 lines for you.  I didn't want to leave you with too much of a challenge to shoot for, to get it even smaller:

Code: QB64: [Select]
  1. 1 FOR i = 1 TO 5
  2.    2 r(i) = INT(RND * TIMER(0.01) * 100) MOD 59 + 1
  3.    3 _PRINTSTRING ((i - 1) * 3 + 1, 1), STR$(r(i))
  4.    4 FOR j = 1 TO i - 1
  5.        5 IF r(i) = r(j) GOTO 2
  6. 6 NEXT j, i
  7. 7 PRINT CHR$(10); "The red Powerball number is:"; INT(RND * 35) + 1
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 10:22:44 pm
Like I said, "I learn." Nice way to get a Randomize Timer effect!
Code: QB64: [Select]
  1. r$ = RIGHT$(SPC(3) + STR$(INT((RND * TIMER)) MOD 69 + 1), 3)
  2. WHILE INSTR(b$, r$) = 0 AND LEN(b$) < 15
  3.     b$ = b$ + r$
  4.     r$ = RIGHT$(SPC(3) + STR$(INT((RND * TIMER)) MOD 69 + 1), 3)
  5. PRINT b$; "  and Red Powerball:"; STR$(INT((RND * TIMER) MOD 26) + 1)
  6.  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 10:42:46 pm
Dang! sometimes the balls come up short, well here's a better 5 liner:
Code: QB64: [Select]
  1. 1 r$ = RIGHT$(SPC(3) + STR$(INT((RND * TIMER)) MOD 69 + 1), 3)
  2. IF INSTR(b$, r$) > 0 THEN GOTO 1 ELSE GOTO 3
  3. 3 b$ = b$ + r$
  4. IF LEN(b$) < 15 THEN GOTO 1 ELSE GOTO 5
  5. 5 PRINT b$; "  and Red Powerball:"; STR$(INT((RND * TIMER) MOD 26) + 1)
  6.  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 27, 2020, 10:53:21 pm
A few bytes less (191):
Code: QB64: [Select]
  1. 1 r$ = RIGHT$(SPC(3) + STR$((INT(RND * TIMER)) MOD 69 + 1), 3)
  2. IF INSTR(b$, r$) > 0 GOTO 1
  3. b$ = b$ + r$
  4. IF LEN(b$) < 15 GOTO 1
  5. PRINT b$; " Red Ball"; STR$((INT(RND * TIMER)) MOD 26 + 1)
  6.  
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: Pete on October 27, 2020, 11:32:45 pm
Quote
Steve Edits(tm)

Can QB64 fix a broken keister? Because that one had me falling out of my chair!

Pete AKA Pat N. Pending
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: NOVARSEG on October 28, 2020, 02:03:20 am
I would not use peek and poke


POKE  A&,B%
A& = an offset from 0 to 65535 which is located in a memory segment specified by
      the most recently executed DEF SEG statement

B%  = a value from 0 to 255 which is written at the offset location

It is better to refer to data by name. Almost any data in memory can be referred to by name.
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: SMcNeill on October 28, 2020, 02:43:28 am
Dang! sometimes the balls come up short, well here's a better 5 liner:
Code: QB64: [Select]
  1. 1 r$ = RIGHT$(SPC(3) + STR$(INT((RND * TIMER)) MOD 69 + 1), 3)
  2. IF INSTR(b$, r$) > 0 THEN GOTO 1 ELSE GOTO 3
  3. 3 b$ = b$ + r$
  4. IF LEN(b$) < 15 THEN GOTO 1 ELSE GOTO 5
  5. 5 PRINT b$; "  and Red Powerball:"; STR$(INT((RND * TIMER) MOD 26) + 1)
  6.  

Thieves "borrow without returning".  Geniuses (like yours truly) steal and laugh maniacally...

Code: QB64: [Select]
  1. 1 r$ = RIGHT$(SPC(3) + STR$((INT(RND * TIMER)) MOD 59 + 1), 3)
  2. IF INSTR(b$, r$) > 0 GOTO 1 ELSE b$ = b$ + r$
  3. IF LEN(b$) < 15 GOTO 1 ELSE PRINT b$; " Red Ball"; STR$((INT(RND * TIMER)) MOD 26 + 1)

Your five liner is now only a three liner.  :P
Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 28, 2020, 08:04:33 am
Code: QB64: [Select]
  1. PRINT STR$((INT(RND * TIMER)) MOD 69 + 1); STR$((INT(RND * TIMER)) MOD 69 + 1); STR$((INT(RND * TIMER)) MOD 69 + 1); STR$((INT(RND * TIMER)) MOD 69 + 1); STR$((INT(RND * TIMER)) MOD 69 + 1); " Red Ball"; STR$((INT(RND * TIMER)) MOD 26 + 1)
  2.  

Probably not always right but what are the odds?

About 86.3% Good!

Code: QB64: [Select]
  1. 1 test$ = STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + " Red Ball" + STR$((INT(RND * TIMER)) MOD 26 + 1)
  2. REDIM p$(0)
  3. Split test$, " ", p$()
  4. noGoodF = 0
  5. FOR i = 1 TO 5
  6.     PRINT p$(i); ", ";
  7.     IF i > 1 THEN
  8.         FOR j = 1 TO i - 1
  9.             IF p$(i) = p$(j) THEN noGoodF = -1
  10.         NEXT
  11.     END IF
  12. IF noGoodF THEN
  13.     PRINT "No Good": NoGood = NoGood + 1
  14.     PRINT "Good!": good = good + 1
  15. testN = testN + 1
  16. IF testN < 100000 THEN GOTO 1
  17. PRINT "Good"; good; ", No Good"; NoGood
  18.  
  19. SUB Split (SplitMeString AS STRING, delim AS STRING, loadMeArray() AS STRING)
  20.     DIM curpos AS LONG, arrpos AS LONG, LD AS LONG, dpos AS LONG 'fix use the Lbound the array already has
  21.     curpos = 1: arrpos = LBOUND(loadMeArray): LD = LEN(delim)
  22.     dpos = INSTR(curpos, SplitMeString, delim)
  23.     DO UNTIL dpos = 0
  24.         loadMeArray(arrpos) = MID$(SplitMeString, curpos, dpos - curpos)
  25.         arrpos = arrpos + 1
  26.         IF arrpos > UBOUND(loadMeArray) THEN REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO UBOUND(loadMeArray) + 1000) AS STRING
  27.         curpos = dpos + LD
  28.         dpos = INSTR(curpos, SplitMeString, delim)
  29.     LOOP
  30.     loadMeArray(arrpos) = MID$(SplitMeString, curpos)
  31.     REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
  32.  
  33.  
  34.  

BTW it is 69 white balls
 https://www.google.com/search?client=opera&q=is+the+red+ball+of+power+ball+separate+from+the+other+69&sourceid=opera&ie=UTF-8&oe=UTF-8
Quote
The first five regular numbers, which are white balls, are drawn out of a drum with 69 balls; one Powerball, or red ball, is drawn from a separate drum with 26 balls. Matching all five regular numbers, or white balls, in any order and the red Powerball will win you the jackpot.Aug 15, 2017

Title: Re: question about PEEK and POKE - hello need to know what this is doing
Post by: bplus on October 28, 2020, 10:29:13 am
Alas, I found a slight flaw with Split ;-(

Oh wait if I trimmed test$, yes I do have a value for first = lower bound index in array, OK :)
Code: QB64: [Select]
  1. 1 test$ = STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + STR$((INT(RND * TIMER)) MOD 69 + 1) + " Red Ball" + STR$((INT(RND * TIMER)) MOD 26 + 1)
  2. REDIM p$(0)
  3. Split _TRIM$(test$), " ", p$()
  4. noGoodF = 0
  5. FOR i = 0 TO 4
  6.     PRINT p$(i); ", ";
  7.     IF i > 0 THEN
  8.         FOR j = 0 TO i - 1
  9.             IF p$(i) = p$(j) THEN noGoodF = -1
  10.         NEXT
  11.     END IF
  12. IF noGoodF THEN
  13.     PRINT "No Good": NoGood = NoGood + 1
  14.     PRINT "Good!": good = good + 1
  15. testN = testN + 1
  16. IF testN < 100000 THEN GOTO 1
  17. PRINT "Good"; good; ", No Good"; NoGood
  18.  
  19. SUB Split (SplitMeString AS STRING, delim AS STRING, loadMeArray() AS STRING)
  20.     DIM curpos AS LONG, arrpos AS LONG, LD AS LONG, dpos AS LONG 'fix use the Lbound the array already has
  21.     curpos = 1: arrpos = LBOUND(loadMeArray): LD = LEN(delim)
  22.     dpos = INSTR(curpos, SplitMeString, delim)
  23.     DO UNTIL dpos = 0
  24.         loadMeArray(arrpos) = MID$(SplitMeString, curpos, dpos - curpos)
  25.         arrpos = arrpos + 1
  26.         IF arrpos > UBOUND(loadMeArray) THEN REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO UBOUND(loadMeArray) + 1000) AS STRING
  27.         curpos = dpos + LD
  28.         dpos = INSTR(curpos, SplitMeString, delim)
  29.     LOOP
  30.     loadMeArray(arrpos) = MID$(SplitMeString, curpos)
  31.     REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
  32.  

That's better use of split.