Author Topic: Using mouse movement to make a "randomizer"?  (Read 3327 times)

0 Members and 1 Guest are viewing this topic.

Offline FilipeEstima

  • Newbie
  • Posts: 63
    • View Profile
Using mouse movement to make a "randomizer"?
« on: September 27, 2020, 11:16:18 pm »
I have zero experience with capturing mouse movements with QB64. I'm thinking about a way to insert some random elements in an array and since computers can't generate true random numbers, I figured maybe I could use input made by mouse movements to help. What are the instructions I need to use for that?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Using mouse movement to make a "randomizer"?
« Reply #1 on: September 27, 2020, 11:22:19 pm »
Code: QB64: [Select]
  1. _TITLE "Mouse x, y is column, row for a character in screen 0"
  2.     WHILE _MOUSEINPUT: WEND '             poll mouse
  3.     mx = _MOUSEX: my = _MOUSEY '          just get locations of mouse
  4.     LOCATE 1, 1: PRINT SPACE$(30) '       clear last location
  5.     LOCATE 1, 1: PRINT "Mouse:"; mx; my ' print current location
  6.     _LIMIT 60 '                           save CPU fan
  7.  
  8.  
  9.  

I look forward to seeing mouse movements converted to random numbers, very interesting!
« Last Edit: September 27, 2020, 11:27:09 pm by bplus »

Offline FilipeEstima

  • Newbie
  • Posts: 63
    • View Profile
Re: Using mouse movement to make a "randomizer"?
« Reply #2 on: September 28, 2020, 10:26:25 pm »
Thanks bplus, I will give a try!

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Using mouse movement to make a "randomizer"?
« Reply #3 on: September 29, 2020, 12:48:11 am »
Here's an idea:
Code: QB64: [Select]
  1. _TITLE "Mouse x, y is column, row for a character in screen 0"
  2.  
  3. sum = 0
  4.     WHILE _MOUSEINPUT: WEND '             poll mouse
  5.     mx = _MOUSEX: my = _MOUSEY '          just get locations of mouse
  6.     LOCATE 1, 1: PRINT SPACE$(30) '       clear last location
  7.     LOCATE 1, 1: PRINT "Mouse:"; mx; my ' print current location
  8.  
  9.  
  10.         if mx<>omx or my<>omy then
  11.                 sum = sum + mx*my
  12.                 randomize sum
  13.                 ? rnd*1000
  14.  
  15.                 omx = mx
  16.                 omy = my
  17.         end if
  18.  
  19.     _LIMIT 60 '                           save CPU fan
  20.  

When you start the program with the same mouse position every time, the number will be the same.  But as you move the mouse around over a long run time you should get less predictable results. Can't comment on stats encryption or 'entropy' whatever, just an idea.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Using mouse movement to make a "randomizer"?
« Reply #4 on: September 29, 2020, 01:36:09 am »
Why not a RANDOMIZE (TIMER * (1 + _MOUSEX) * (1 + _MOUSEY))?  I’d imagine that’d be a little difficult to ever predict exactly what seed might be used to generate your seed number.

(And note the +1 to both mousex and mousey...  This is to prevent us multiplying by 0, giving skewed, multiply repetitive results.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!