Discussion:
MathObject.Random?
(too old to reply)
Stefan Ram
2015-12-28 05:59:26 UTC
Permalink
I would like to call a random number method that returns a double.

For certain reasons, I'd like to call a predefined method,
not a method written by me. Otherwise It would be easy.

I thought that the method »random« on this page would fit:

msdn.microsoft.com/en-us/library/41336409(v=vs.94).aspx

. So, I tried to refer to it:

csc /reference:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.JScript.dll Program.cs

. However, I cannot discover any types from this assembly
(but I do not get an error message either.)

This small program tries to discover the JScript assembly:

public sealed class Program
{ public static void Main()
{ System.Reflection.Assembly[] assemblies =
System.AppDomain.CurrentDomain.GetAssemblies();
foreach( System.Reflection.Assembly assembly in assemblies )
{ System.Console.Out.WriteLine( "Assembly: {0}", assembly );
foreach( System.Type type in assembly.GetExportedTypes())
{ System.Console.Out.WriteLine( "Type: {0}", type ); }}}}

It shows »mscorlib« and »Program«, but I'd also like to see »JScript«.

Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Type: System.Object
...
Type: System.Math
...
Assembly: Program, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Type: Program

»System.Math« is there, but not other type with »Math« in
its name.

Can you explain to me what I need to do in order to access
the JScript Math.random() method from my C# program?
(A similar J# method also would do it.)
Stefan Ram
2015-12-28 06:08:09 UTC
Permalink
Post by Stefan Ram
It shows »mscorlib« and »Program«, but I'd also like to see »JScript«.
In the meantime, I found out that this will work

class Example
{ static void Main()
{ System.Console.WriteLine
( Microsoft.JScript.MathObject.random() ); }}

even though my previous program did not show the type!
Marcel Mueller
2015-12-28 18:41:48 UTC
Permalink
Post by Stefan Ram
I would like to call a random number method that returns a double.
For certain reasons, I'd like to call a predefined method,
System.Random.NextDouble() is your friend.


Marcel
Stefan Ram
2015-12-28 18:50:13 UTC
Permalink
Post by Marcel Mueller
Post by Stefan Ram
I would like to call a random number method that returns a double.
For certain reasons, I'd like to call a predefined method,
System.Random.NextDouble() is your friend.
I need it for my C# course. But at that point in the course,
I have only introduced /static/ methods. So I »can't« yet
call non-static methods.

In fact, »random()« is intended to be the first example of a
method call in the whole course.

I am glad that I now found

/* Microsoft.JScript.dll */
global::Microsoft.JScript.MathObject.random()

But I still do not understand why the program in my OP
does not »see« all types that are available to it.
Arne Vajhøj
2015-12-29 01:17:12 UTC
Permalink
Post by Stefan Ram
Post by Marcel Mueller
Post by Stefan Ram
I would like to call a random number method that returns a double.
For certain reasons, I'd like to call a predefined method,
System.Random.NextDouble() is your friend.
I need it for my C# course. But at that point in the course,
I have only introduced /static/ methods. So I »can't« yet
call non-static methods.
In fact, »random()« is intended to be the first example of a
method call in the whole course.
I am glad that I now found
/* Microsoft.JScript.dll */
global::Microsoft.JScript.MathObject.random()
So you avoid bringing up:
- instances of classes
- non-static methods.

And instead you bring up:
- namespaces
- assemblies
- something that is intended to be used by the runtime of a
different language not from C# code
- something non-portable

That seems like a very bad trade-off to me.

Arne
Stefan Ram
2015-12-29 01:42:54 UTC
Permalink
Post by Arne Vajhøj
- instances of classes
- non-static methods.
Both will be explained later in the course.
Post by Arne Vajhøj
- namespaces
- assemblies
Namespaces are required right from the start,
just to explain »global::System.Console.Out.WriteLine«.

Assemblies are required right from the start, alone to
explain what Csc does (it takes a source file and creates an
assembly from it).
Post by Arne Vajhøj
- something that is intended to be used by the runtime of a
different language not from C# code
Ok, that I have to admit. Even the documentation says that
one is not supposed to call it directly from user code.
Post by Arne Vajhøj
- something non-portable
When we are writing code in VBA, JavaScript, or Java, there
always is a static random method for 0.0 <= x < 1.0. But C#,
C, and C++ do not have such a method in their standard
libraries. So it's rather portable among about half the
languages that come to my mind right now. And thanks to
»Microsoft.JScript«, now C# also belongs to that group of
languages with a static double random method until Microsoft
will take »Microsoft.JScript« away from us (from the standard
Dotnet distribution). And I believe I saw that this
»Microsoft.JScript« also exists in Mono.
Marcel Mueller
2015-12-29 17:52:28 UTC
Permalink
Post by Stefan Ram
When we are writing code in VBA, JavaScript, or Java, there
always is a static random method for 0.0 <= x < 1.0. But C#,
C, and C++ do not have such a method in their standard
libraries.
What a pity!

static class StaticRandom
{ static readonly Random rnd = new Random();
public static double NextDouble()
{ return rnd.NextDouble(); }
}


Marcel
Arne Vajhøj
2015-12-30 03:32:53 UTC
Permalink
Post by Stefan Ram
Post by Arne Vajhøj
- instances of classes
- non-static methods.
Both will be explained later in the course.
Post by Arne Vajhøj
- namespaces
- assemblies
Namespaces are required right from the start,
just to explain »global::System.Console.Out.WriteLine«.
Not required - just chosen.

Using System at top and Console.WriteLine later would
make it a simple "just put using System at top:.
Post by Stefan Ram
Assemblies are required right from the start, alone to
explain what Csc does (it takes a source file and creates an
assembly from it).
It does.

But it is much simpler to just explain that csc produces an EXE
file.

It is not required to explain that the EXE is really a .NET
assembly and how it get treated a bit different than a traditional
EXE file.
Post by Stefan Ram
Post by Arne Vajhøj
- something that is intended to be used by the runtime of a
different language not from C# code
Ok, that I have to admit. Even the documentation says that
one is not supposed to call it directly from user code.
Yes.
Post by Stefan Ram
Post by Arne Vajhøj
- something non-portable
When we are writing code in VBA, JavaScript, or Java, there
always is a static random method for 0.0 <= x < 1.0. But C#,
C, and C++ do not have such a method in their standard
libraries. So it's rather portable among about half the
languages that come to my mind right now.
Source code is usually not portable between languages, so that
is not a big concern.

But I will strongly recommend to use only System namespaces
and not use Microsoft namespaces unless you have very strong
reasons to.

It is not quite as bad as using sun packages in Java (in .NET they
are actually documented/supported), but it is in that
direction.
Post by Stefan Ram
And thanks to
»Microsoft.JScript«, now C# also belongs to that group of
languages with a static double random method until Microsoft
will take »Microsoft.JScript« away from us (from the standard
Dotnet distribution). And I believe I saw that this
»Microsoft.JScript« also exists in Mono.
It does but it is not complete:

http://go-mono.com/status/status.aspx?reference=4.0&profile=4.0&assembly=Microsoft.JScript

Arne

Marcel Mueller
2015-12-29 17:45:23 UTC
Permalink
Post by Arne Vajhøj
- namespaces
- assemblies
- something that is intended to be used by the runtime of a
different language not from C# code
- something non-portable
That seems like a very bad trade-off to me.
Full Ack!


Marcel
Arne Vajhøj
2015-12-29 01:24:29 UTC
Permalink
Post by Stefan Ram
But I still do not understand why the program in my OP
does not »see« all types that are available to it.
Your original code basically did:

for all loaded assemblies {
print assembly name
for all types in assembly {
print type name
}
}

But you did not load Microsoft.JScript.dll, so it was not there.

csc /r:Microsoft.JScript.dll

is somewhat the same as:

javac -cp Microsoft.JScript.jar

in Java.

Neither include anything unless needed.

Arne
Loading...