samedi 16 avril 2022

Weird behaviour of random with double click on batch script

Alright, hello everyone

I feel very confused right now, I would like to have some insight on what is going on. I am trying to write a batch script that would launch a random web browser when I double-click the icon. Easy enough.

Here's my first attempt:

set /a var=%random% %%7 + 1

if %var%==1 (start "brave" "C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe")
if %var%==2 (start "chrome" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
if %var%==3 (start "edge" "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
if %var%==4 (start "firefox" "C:\Program Files\Mozilla Firefox\firefox.exe")
if %var%==5 (start "libreWolf" "C:\Program Files\LibreWolf\librewolf.exe")
if %var%==6 (start "opera" "C:\Program Files\Opera\opera.exe")
if %var%==7 (start "vivaldi" "C:\Program Files\Vivaldi\Application\vivaldi.exe")

This works perfectly, nothing to complain about.

Now, I wanted to make my random picking a little bit cleaner, so I did this:

set /a minval=1
set /a maxval=7
set /a var=%RANDOM% * (%maxval% - %minval% + 1) / 32768 + %minval%

And here is where the strange stuff is happening.

When I double-click the icon, it keeps opening the same browser on and on (meaning, and I verified it, that var always has the same value). But when I start the script from the cmd, it works perfectly, as before.

What is going on?! How is the first attempt okay but not the second one? And why does it work from the command line and not from mouse input?

P.S.: Sorry for my english, please be nice :)




Aucun commentaire:

Enregistrer un commentaire