Skip to content

Instantly share code, notes, and snippets.

@hurrifan1
Last active July 19, 2020 20:22
Show Gist options
  • Save hurrifan1/c56d9479464226b889916def0cf8962d to your computer and use it in GitHub Desktop.
Save hurrifan1/c56d9479464226b889916def0cf8962d to your computer and use it in GitHub Desktop.
This is a Dart script that generates a random alphanumeric string of variable length.
import 'dart:core';
import 'dart:math';
String getRandString(len) {
// Initialization of the string
var a = '';
// Generate a list of UTF-16 code units that
// are limited to just numbers and uppercase letters.
// Thus, we filter out the code units for non-alphanumeric
// characters (58:64, inclusive).
var y = Iterable.generate(42, (int x) => 48 + x++)
.where((int z) => (z < 58) || (z > 64))
.toList();
// Grab a random alphanumeric character by generating a random
// integer between 1 and the length of the UTF code unit list
// created above and then using that to get the code unit indexed
// at that integer.
for (int x = 1; x <= len; x++) {
int r = Random().nextInt(y.length);
int w = y[r];
a += String.fromCharCode(w);
}
return a;
}
void main() {
int x = 10;
print('Random value of length $x = ${getRandString(x)}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment