Re: How to generate a random number from system clock as seed

2024-06-08 Thread monkyyy via Digitalmars-d-learn

On Saturday, 8 June 2024 at 20:53:02 UTC, Nick Treleaven wrote:

On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote:
rng is an optional parameter, `uniform(0,100).writeln;` alone 
works; the docs not telling you that is really bad


They do tell you:

urng 	(optional) random number generator to use; if not 
specified, defaults to rndGen


That overload is used in the 2nd example.

https://dlang.org/phobos/std_random.html#.uniform


generate is a very rare function and do novices understand lamdas?


Re: How to generate a random number from system clock as seed

2024-06-08 Thread Nick Treleaven via Digitalmars-d-learn

On Saturday, 8 June 2024 at 16:09:04 UTC, monkyyy wrote:
rng is an optional parameter, `uniform(0,100).writeln;` alone 
works; the docs not telling you that is really bad


They do tell you:

urng 	(optional) random number generator to use; if not 
specified, defaults to rndGen


That overload is used in the 2nd example.

https://dlang.org/phobos/std_random.html#.uniform



Re: How to generate a random number from system clock as seed

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 8 June 2024 at 18:25:20 UTC, drug007 wrote:


```d
{
const seed = castFrom!long.to!uint(Clock.currStdTime);
auto rng = Random(seed);
auto result = generate!(() => uniform(0, 10, 
rng))().take(7);

// new random numbers sequence every time
result.writeln;
}
```


If UnixTime is desired, it is sufficient to have 
currTime.toUnixTime instead of currStdTime. It will not reset 
until January 19, 2038.


```d
auto seed = to!uint(Clock.currTime.toUnixTime);
```

SDB@79


Re: How to generate a random number from system clock as seed

2024-06-08 Thread drug007 via Digitalmars-d-learn

On 08.06.2024 16:19, Eric P626 wrote:

I managed to create a random number generator using the following code:

~~~
auto rng = Random(42);
//
uniform(0,10,rng);
~~~

Now I want to seed the generator using system time. I looked at Date & 
time functions/classes and systime functions/classes. The problem is 
that they all require a time zone. But I don't need a time zone since 
there is no time zone. I just want the number of seconds elapsed since 
jan 1st 1970. In other words, the internal system clock value.




```d
import std;

void main()
{
{
auto rng = Random(42);
auto result = generate!(() => uniform(0, 10, rng))().take(7);
// the same random numbers sequence
assert (result.equal([2, 7, 6, 4, 6, 5, 0]));
}

{
const seed = castFrom!long.to!uint(Clock.currStdTime);
auto rng = Random(seed);
auto result = generate!(() => uniform(0, 10, rng))().take(7);
// new random numbers sequence every time
result.writeln;
}
}
```


Re: How to generate a random number from system clock as seed

2024-06-08 Thread monkyyy via Digitalmars-d-learn

On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote:
I managed to create a random number generator using the 
following code:


~~~
auto rng = Random(42);
//
uniform(0,10,rng);
~~~

Now I want to seed the generator using system time. I looked at 
Date & time functions/classes and systime functions/classes. 
The problem is that they all require a time zone. But I don't 
need a time zone since there is no time zone. I just want the 
number of seconds elapsed since jan 1st 1970. In other words, 
the internal system clock value.


rng is an optional parameter, `uniform(0,100).writeln;` alone 
works; the docs not telling you that is really bad


the docs/api for std.time/random are bad if you need something 
specif Id suggest doing it yourself, but if you want to use 
std.time anyway the magic word I think is "localtime"(Ive pounded 
my head into those auto generated docs and had to dive deep to 
find such estoric knowledge)


if you need a spefic random number from a spefic timestamp, Id 
suggest making a rng function from scratch and using clibs time 
stuff


Re: How to generate a random number from system clock as seed

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 8 June 2024 at 13:19:30 UTC, Eric P626 wrote:
I just want the number of seconds elapsed since jan 1st 1970. 
In other words, the internal system clock value.


#unix #time

@SDB79



How to generate a random number from system clock as seed

2024-06-08 Thread Eric P626 via Digitalmars-d-learn
I managed to create a random number generator using the following 
code:


~~~
auto rng = Random(42);
//
uniform(0,10,rng);
~~~

Now I want to seed the generator using system time. I looked at 
Date & time functions/classes and systime functions/classes. The 
problem is that they all require a time zone. But I don't need a 
time zone since there is no time zone. I just want the number of 
seconds elapsed since jan 1st 1970. In other words, the internal 
system clock value.




Re: modInverse & powMod

2024-06-08 Thread Salih Dincer via Digitalmars-d-learn

On Friday, 7 June 2024 at 13:43:29 UTC, Salih Dincer wrote:


SDB@79

I have only one question: Is there a modInverse() function in 
the standard library for use in RSA?


Apparently not, it fell to lot :)

I already had such a function...

```d
auto modInverse(T)(T m, T n) pure
{
  T q, ilk = n;
  T y, tmp, x = 1;

  while (m > 1)
  {
q = m / n;

tmp = n;
  n = m % n;
  m = tmp;

tmp = y;
  y = x - q * y;
  x = tmp;
  }
  return x < 0 ? x + ilk : x;
}
```

And in the BigInt module there was divMod() next to powmod():

```d
auto modInverse(BigInt a, BigInt m)
{
  BigInt q, m0 = m;
  BigInt tmp, y, x = 1;

  while (a > 1)
  {
// m is remainder now
    tmp = m;
    divMod(a, m, q, m);

    // process same as Euclid's algorithm
    a = tmp;
tmp = y;

// Update y and x
y = x - q * y;
x = tmp;
  }

  // Make x positive
  if (x < 0) x += m0;

  return x;
}
```

Is PR required? Why not modInverse too!

SDB@79


Re: How do I install a non-outdated D compiler on Linux? (not in userspace-container)

2024-06-08 Thread solidstate1991 via Digitalmars-d-learn

On Saturday, 8 June 2024 at 03:36:05 UTC, bachmeier wrote:
For Mint, I'd use the .deb and let it handle that stuff. For 
LDC, I have a bash alias for ldmd2 that points to the ldmd2 
binary. Of course there are multiple ways to handle this, but I 
don't understand the point of the install script, since it 
leaves you without a working installation.


Okay, does someone know how to set up the path for LDC on Linux, 
without the bash alias?


Re: Trying to call some C code using Extern(C) but got unexpected linker error

2024-06-08 Thread Basile B. via Digitalmars-d-learn

On Saturday, 8 June 2024 at 02:22:00 UTC, Xiaochao Yan wrote:
Hi, I am new to D and is experimenting with game development 
using D and C.


I had some problem when trying to recreate the
https://dlang.org/spec/importc.html

[...]

Thanks in advance!


on a recent compiler this should work:

```d
// .d file
import std.stdio;
import std.conv;
import test;

void main() {
writeln("Hello, World!");

pragma(mangle, "printMessage");
extern (C) void printMessage();

printMessage();
}
```

This was fixed by the dear GH Ghost in 
https://github.com/dlang/dmd/pull/15582.
A short technical explanation: mangling and call conventions are 
not 100% related.

You need to stuff the mangle for that to work.