3

I'm trying to run below vbscript in Linux using Wine, but it doesn't work. Other vbscript functions are working as expected.

Wine Version : wine-5.0.3 (Ubuntu 5.0.3-3) Command : wine cscript ./test_rnd.vbs

VBS:

   On Error Resume Next
WScript.Echo "Before Rnd"

Rnd -1

WScript.Echo "After Rnd"

If Err.Number <> 0 Then
  WScript.Echo "Error : " & Err.Number & ": " & Err.Description
End If

OutPut using Wine:

enter image description here

This script is working fine in Windows.

Do i need to install any other wine dependency ? if you have any alternative solution to execute VBS in Linux please mention.

Ara
  • 63
  • 1
  • 7
  • Can you describe what "doesn't work" means? What does the function return? Or does it show an error? Is `Err` set? – CherryDT Apr 25 '21 at 08:27
  • Execution doesn't continue after the "Rnd -1" line. The purpose of this function is to return a random number based on the seed value that we are setting here "Randomize 3922004" – Ara Apr 25 '21 at 08:35
  • What do you mean it doesn't continue? Any console output? What exit code does cscript.exe return? if you add an `On Error` handler, what error does `Err` contain? – CherryDT Apr 25 '21 at 09:58
  • Maybe you should first seed the RNG _before_ you attempt to use it? – CherryDT Apr 25 '21 at 09:59
  • 1
    @CherryDT I updated the question with output – Ara Apr 25 '21 at 11:34
  • 1
    Does it work when you `Randomize` before you try `Rnd`? I can't find any information about this online, I guess a possibility is that it's just not implemented. Maybe you should ask at the [Wine forums](https://forum.winehq.org/viewforum.php?f=8) too. – CherryDT Apr 25 '21 at 13:23
  • [`Randomize`](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/38d7ckek(v=vs.84)) and [`Rnd()`](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/e566zd96(v=vs.84)) use the OS system timer to generate the seed which will be a different implementation in a Linux environment. – user692942 Apr 25 '21 at 16:24

2 Answers2

1

The Rnd() function is dependent on seeds generated by the Randomize statement which generates seeds via the OS System Timer, a Windows OS feature. While Linux will have something similar, VBScript's code base will not know how to call it and will depend on Wine to provide an implementation that mimics the Windows System Timer.

As the error you are receiving is

Object doesn’t support this action

I’m afraid you’re out of luck until Wine provides an implementation.

user692942
  • 16,398
  • 7
  • 76
  • 175
1

Although it does not appear Wine supports VBScript's Rnd function, what about your own implementation of a "random" function with something like this?

Function RndNum(MaxValue)
    RndNum = (((Hour(Now) + 1) * (Minute(Now) + 1) * (Second(Now) + 1) * Right(Timer, 5)) Mod MaxValue)
End Function

How to manually generate random numbers

Edit:

The above code did not generate random numbers when ran in a loop, as the timestamp is not precise enough.

enter image description here

Updating the function to use an external Seed does work, however I would not not vouch for how unique these would be, I'm assuming this would not be for Cryptography...

Function RndNum(Seed, MaxValue)
    RndNum = (((Hour(Now) + 1) * (Minute(Now) + 1) * (Second(Now) + 1) * Right((Seed * Right(Timer, 5)), 8)) Mod MaxValue) 
End Function

Do
    counter = counter + 1
    Numbers = Numbers & RndNum(counter,1000) & ", "
Loop While counter < 10

MsgBox Numbers

enter image description here

Moir
  • 379
  • 4
  • 14
  • While a valiant attempt, it is not an equivalent, because 1. VBScript timing precision isn't accurate enough. 2. The system timer is used to generate a seed or takes a starting seed to be used with the system timer when generating random values. – user692942 Apr 25 '21 at 22:30
  • @user692942 Granted I'm sure these would likely not be a random as the Rnd Function, but what are your thoughts after using the loop counter as an external seed? Depending on the need my thought is this may be random enough. – Moir Apr 26 '21 at 00:19
  • It depends on the users requirements for using it. – user692942 Apr 26 '21 at 07:04