Created
August 30, 2018 02:07
-
-
Save seanvikoren/ad3b5946306ed7b0e27f5b9aa2069fa0 to your computer and use it in GitHub Desktop.
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
import 'dart:math'; | |
class RandomDigits { | |
static const MaxNumericDigits = 17; | |
static final _random = Random(); | |
static int getInteger(int digitCount) { | |
if (digitCount > MaxNumericDigits || digitCount < 1) throw new RangeError.range(0, 1, MaxNumericDigits, "Digit Count"); | |
var digit = _random.nextInt(9) + 1; // first digit must not be a zero | |
int n = digit; | |
for (var i = 0; i < digitCount - 1; i++) { | |
digit = _random.nextInt(10); | |
n *= 10; | |
n += digit; | |
} | |
return n; | |
} | |
static String getString(int digitCount) { | |
var digit = _random.nextInt(9) + 1; // first digit must not be a zero | |
String s = digit.toString(); | |
for (var i = 0; i < digitCount - 1; i++) { | |
s += _random.nextInt(10).toString(); | |
} | |
return s; | |
} | |
} | |
void main() { | |
print(RandomDigits.getInteger(6)); | |
print(RandomDigits.getString(36)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment