From 4ed8180bc79c1cebe1fbd6bf58ae6f5495f0448e Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Tue, 9 Aug 2022 14:17:26 -0700 Subject: [PATCH] Make `Rand` deterministic (#1910) Also re-enable auto-updating of Rand test (disabled in #1894) --- explorer/interpreter/interpreter.cpp | 6 ++++-- explorer/testdata/random/random.carbon | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 088954a7e317..4026b141885f 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -1213,8 +1213,10 @@ auto Interpreter::StepExp() -> ErrorOr { CARBON_CHECK(args.size() == 2); const auto& low = cast(*args[0]).value(); const auto& high = cast(*args[1]).value(); - std::uniform_int_distribution<> distr(low, high); - int r = distr(generator); + CARBON_CHECK(high > low); + // We avoid using std::uniform_int_distribution because it's not + // reproducible across builds/platforms. + int r = (generator() % (high - low)) + low; return todo_.FinishAction(arena_->New(r)); } case IntrinsicExpression::Intrinsic::IntEq: { diff --git a/explorer/testdata/random/random.carbon b/explorer/testdata/random/random.carbon index 6080b1c90718..ee99e7c88651 100644 --- a/explorer/testdata/random/random.carbon +++ b/explorer/testdata/random/random.carbon @@ -6,7 +6,8 @@ // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \ // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s -// NOAUTOUPDATE +// AUTOUPDATE: %{explorer} %s +// CHECK: Nice! // CHECK: result: 0 package ExplorerTest api;