7

[Dart] Generate Random String From [a-z0-9]

 2 years ago
source link: http://siongui.github.io/2017/01/22/dartlang-generate-random-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Dart] Generate Random String From [a-z0-9]

January 22, 2017

import 'dart:core';
import 'dart:math';

const chars = "abcdefghijklmnopqrstuvwxyz0123456789";

String RandomString(int strlen) {
  Random rnd = new Random(new DateTime.now().millisecondsSinceEpoch);
  String result = "";
  for (var i = 0; i < strlen; i++) {
    result += chars[rnd.nextInt(chars.length)];
  }
  return result;
}

void main() {
  print(RandomString(10));
  print(RandomString(20));
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK