random method Null safety

List random(
  1. List shape,
  2. {int start = 0,
  3. int end = 100,
  4. String? dtype = 'int'}
)

Implementation

List random(List shape,
    {int start = 0, int end = 100, String? dtype = 'int'}) {
  math.Random random = new math.Random();
  int mult = 1;
  for (int i = 0; i < shape.length; i++) {
    mult = mult * shape[i] as int;
  }
  List temp = [];
  var val;
  for (int i = 0; i < mult; i++) {
    if (dtype == 'int') {
      val = random.nextInt(end - start);
      temp.add(val > start ? val : val + start);
    } else if (dtype == 'double') {
      val = random.nextDouble() * (end - start) + start;
      temp.add(val);
    } else {
      throw new Exception(
          "DartTensorException : Undefined dtype. Choose 'int' or 'double'.");
    }
  }
  temp = generate(temp, shape);
  return temp;
}