Conversation
A uniformly distributed integer between zero and RANGE, both included. The bounds are measured rather than inferred. The parameter documentation says "The random int will be <= range" and says nothing about the lower bound, and the obvious reading — one to range — is wrong. Three hundred calls per range on an AS ABAP 1909 sandbox: range 1 values 0..1 zero in 163 (a half) range 2 values 0..2 zero in 105 (a third) range 6 values 0..6 zero in 53 (a seventh) range 100 values 0..100 zero in 3 (a hundred-and-first) So the span is RANGE + 1 values. A range of zero answers zero, and a negative range is not an error there either: -5 answered -2, so the span runs towards RANGE on whichever side of zero it lies. The module declares no exceptions and the real one raises none. Written against Math.random directly rather than through cl_abap_random_int, which was the first attempt and does not fit: cl_abap_random=>intinrange opens with ASSERT high > low and ASSERT low >= 0, so a range of zero dumps and every negative range dumps. Reaching the contract through abs( ) and a negation would work and would read as cleverness hiding the intent. It is the same Math.random either way, since that is what cl_abap_random uses, and generate_sec_random in this group is the precedent for the plain form. Four tests in kernel_fugr_test: the zero range, the bounds at 6 and at -5, and that both 0 and 1 are reachable at range 1 — the last one being what tells this apart from a generator over 1..RANGE.
Member
|
hmm, might also need to go into deprecated? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A uniformly distributed integer between zero and
RANGE, both included.The bounds are measured, not inferred
The parameter documentation says "The random int will be <= range" and says nothing about the lower bound. The obvious reading — one to range — is wrong. Three hundred calls per range on an AS ABAP 1909 sandbox:
So the span is
RANGE + 1values, and the zero frequency matches1/(range+1)at every point. Edges, same system:range = 0answered0;range = -5answered-2, so a negative range is not an error and the span runs towardsRANGEon whichever side of zero it lies; 2000 draws atrange = 1gave 1002 zeroes. The module declares no exceptions and the real one raises none.The numbers come from calling the module, from a throwaway report and test class in
$TMP, not from reading its implementation — an observation can be contributed, an adaptation cannot.Why not
cl_abap_random_intThat was the first attempt and it does not fit.
cl_abap_random=>intinrangeopens withASSERT high > lowandASSERT low >= 0, so a range of zero dumps and every negative range dumps. Reaching the measured contract throughabs( )and a negation works and reads as cleverness hiding the intent.It is the same
Math.randomeither way — that is whatcl_abap_randomuses — andgenerate_sec_randomin this same function group is the precedent for the plain@KERNELform.Tests
Four, in
kernel_fugr_test: the zero range, the bounds at 6 and at -5, and that both 0 and 1 are reachable at range 1. The last is the one that tells this apart from a generator over1..RANGE; with the naive implementation in place it fails withExpected '0', got '1', which is how it was checked.Where it came from
An ABAP Z-machine interpreter. MiniZork plays twenty-five commands and then dies on the troll, because the
randomopcode calls this module and getsCX_SY_DYN_CALL_ILLEGAL_FUNC. With it the whole walkthrough plays.Worth noting for anyone porting a Z-machine: the Z standard wants
1..range, and this module gives0..range, so the caller has to map. That is the caller's job and not this module's — matching the real system is.🤖 Generated with Claude Code
https://claude.ai/code/session_01J7JpA3TGT48zBoix3Ut88s