Author Topic: question about PEEK and POKE - hello need to know what this is doing  (Read 5785 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #15 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?

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #16 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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #17 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 ;-))

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #18 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
« Last Edit: October 27, 2020, 07:40:58 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #19 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!
« Last Edit: October 27, 2020, 07:43:08 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #20 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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #21 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!" 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #22 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 ;-))

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #23 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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #24 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.  
« Last Edit: October 27, 2020, 10:46:03 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #25 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #26 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.  
« Last Edit: October 27, 2020, 11:01:51 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #27 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #28 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.
« Last Edit: October 28, 2020, 02:30:33 am by NOVARSEG »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #29 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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!