-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from Standard.Base import all | ||
|
||
polyglot java import java.util.Random | ||
|
||
upper_case_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".utf_8 | ||
numbers = "0123456789".utf_8 | ||
|
||
## Creates a random number generator which can be used for creating test values. | ||
|
||
Arguments: | ||
- seed: Optional seed value to make the sequence deterministic | ||
make_generator : Integer -> Random | ||
make_generator (seed = 0) = | ||
if seed == 0 then Random.new else Random.new seed | ||
|
||
|
||
## Creates a random string based on a template and random number generator. | ||
|
||
Arguments: | ||
- template: Vector of character arrays that represent the possible | ||
characters for each letter. | ||
- generator: Random number generator | ||
|
||
> Examples: | ||
Creates a fake UK National Insurance number: | ||
|
||
l = "ABCEGHJKLMNOPRSTWXYZ".utf_8 | ||
n = "0123456789".utf_8 | ||
s = "ABCDFMP ".utf_8 | ||
template = [l, l, n, n, n, n, n, s] | ||
ni_number = make_string template make_generator | ||
make_string : Vector -> Any -> Text | ||
make_string template generator = | ||
output = Array.new template.length | ||
0.up_to template.length . each i-> | ||
a = template.at i | ||
output.set_at i (a.at (generator.nextInt a.length)) | ||
Text_Utils.from_utf_8 output |