QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: FilipeEstima on September 27, 2020, 11:16:18 pm

Title: Using mouse movement to make a "randomizer"?
Post by: FilipeEstima 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?
Title: Re: Using mouse movement to make a "randomizer"?
Post by: bplus 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!
Title: Re: Using mouse movement to make a "randomizer"?
Post by: FilipeEstima on September 28, 2020, 10:26:25 pm
Thanks bplus, I will give a try!
Title: Re: Using mouse movement to make a "randomizer"?
Post by: _vince 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.
Title: Re: Using mouse movement to make a "randomizer"?
Post by: SMcNeill 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.)