Thread overview
std.random.uniform(1, 101) crashes. Why, and workaround.
Jul 25
monkyyy
Jul 25
evilrat
July 25

From "Programming in D" book:

import std.stdio;
import std.random;

void main() {
	int number = uniform(1, 101);

	writeln("Edit source/app.d to start your project.");
}

Running it generates:
phobos64.lib(random_6ea_855.obj) : error LNK2019: unresolved external symbol BCryptGenRandom referenced in function _D3std6random__T15bcryptGenRandomTmZQuFNbNiNeJmZb
C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
       C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\link.exe /NOLOGO "C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\objs\app.exe.obj" /OUT:"C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp"  /DEFAULTLIB:phobos64  /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\x64" legacy_stdio_definitions.lib /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.26100.0\um\x64"
Error: undefined reference to `BCryptGenRandom`
       referenced from `nothrow @nogc @trusted bool std.random.bcryptGenRandom!(ulong).bcryptGenRandom(out ulong)`
       perhaps a library needs to be added with the `-L` flag or `pragma(lib, ...)`

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'rdmd' 'c:\dev\D\21 - 30\c27_a_do_while_loop\source\app.d'" terminated with exit code: 1.

Is there another way to get a random integer between 1 and 100?

July 24
On Fri, Jul 25, 2025 at 01:04:31AM +0000, Brother Bill via Digitalmars-d-learn wrote:
> From "Programming in D" book:
> 
> import std.stdio;
> import std.random;
> 
>     void main() {
>     	int number = uniform(1, 101);
> 
>     	writeln("Edit source/app.d to start your project.");
>     }
> 
>     Running it generates:
>     phobos64.lib(random_6ea_855.obj) : error LNK2019: unresolved external
> symbol BCryptGenRandom referenced in function
> _D3std6random__T15bcryptGenRandomTmZQuFNbNiNeJmZb
> C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp : fatal error LNK1120: 1 unresolved externals
>     Error: linker exited with status 1120
[...]

You might want to check your compiler installation. Linker errors from the standard library usually mean one or more parts of the compiler toolchain wasn't installed correctly.  Or you may have a stale D installation from before, and for whatever reason the current compiler is wrongly picking up older versions of the standard library, which is incompatible with the current version.

Maybe run dmd with -v and look at the path(s) it's using for the standard libraries and runtime files.  If these don't look like where the (current) compiler is installed, it's probably why you're getting linker errors.


T

-- 
Creativity is not an excuse for sloppiness.
July 25

On Friday, 25 July 2025 at 01:04:31 UTC, Brother Bill wrote:

>

_D3std6random__T15bcryptGenRandomTmZQuFNbNiNeJmZb

linking is different then a runtime crash; restart your computer, reinstall, pray to dark gods, double check your install method was the intended way, try the backup install script, confess your sins for worshiping the dark gods, check the env variables

July 25

On Friday, 25 July 2025 at 01:04:31 UTC, Brother Bill wrote:

>

From "Programming in D" book:

import std.stdio;
import std.random;

void main() {
	int number = uniform(1, 101);

	writeln("Edit source/app.d to start your project.");
}

Running it generates:
phobos64.lib(random_6ea_855.obj) : error LNK2019: unresolved external symbol BCryptGenRandom referenced in function _D3std6random__T15bcryptGenRandomTmZQuFNbNiNeJmZb
C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
       C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\link.exe /NOLOGO "C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\objs\app.exe.obj" /OUT:"C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp"  /DEFAULTLIB:phobos64  /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\x64" legacy_stdio_definitions.lib /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.26100.0\um\x64"
Error: undefined reference to `BCryptGenRandom`
       referenced from `nothrow @nogc @trusted bool std.random.bcryptGenRandom!(ulong).bcryptGenRandom(out ulong)`
       perhaps a library needs to be added with the `-L` flag or `pragma(lib, ...)`

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'rdmd' 'c:\dev\D\21 - 30\c27_a_do_while_loop\source\app.d'" terminated with exit code: 1.

Is there another way to get a random integer between 1 and 100?

could be this:
Windows link error with std.random.uniform() in v2.111 #10731
https://github.com/dlang/phobos/issues/10731

July 25

On Friday, 25 July 2025 at 07:16:55 UTC, evilrat wrote:

>

On Friday, 25 July 2025 at 01:04:31 UTC, Brother Bill wrote:

>

From "Programming in D" book:

import std.stdio;
import std.random;

void main() {
	int number = uniform(1, 101);

	writeln("Edit source/app.d to start your project.");
}

Running it generates:
phobos64.lib(random_6ea_855.obj) : error LNK2019: unresolved external symbol BCryptGenRandom referenced in function _D3std6random__T15bcryptGenRandomTmZQuFNbNiNeJmZb
C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
       C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\bin\HostX64\x64\link.exe /NOLOGO "C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\objs\app.exe.obj" /OUT:"C:\Users\broth\AppData\Local\Temp\.rdmd\rdmd-app.d-99D1A2446E2CA5089AD8A9B31EA74F07\app.exe.tmp"  /DEFAULTLIB:phobos64  /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.44.35207\lib\x64" legacy_stdio_definitions.lib /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.26100.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.26100.0\um\x64"
Error: undefined reference to `BCryptGenRandom`
       referenced from `nothrow @nogc @trusted bool std.random.bcryptGenRandom!(ulong).bcryptGenRandom(out ulong)`
       perhaps a library needs to be added with the `-L` flag or `pragma(lib, ...)`

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'rdmd' 'c:\dev\D\21 - 30\c27_a_do_while_loop\source\app.d'" terminated with exit code: 1.

Is there another way to get a random integer between 1 and 100?

could be this:
Windows link error with std.random.uniform() in v2.111 #10731
https://github.com/dlang/phobos/issues/10731

Regarding https://github.com/dlang/phobos/issues/10731
DMD Version v2.111.1 will fix this, but hasn't been published yet.