randomInt property

DartBlockNativeFunction randomInt
final

Generate a random integer between min (inclusive) and max (inclusive).

Implementation

static final randomInt = DartBlockNativeFunction(
  name: 'randomInt',
  returnType: DartBlockDataType.integerType,
  parameters: [
    DartBlockVariableDefinition('min', DartBlockDataType.integerType),
    DartBlockVariableDefinition('max', DartBlockDataType.integerType),
  ],
  implementation: (arbiter, args) {
    final min = args[0].getValue(arbiter) as int;
    final max = args[1].getValue(arbiter) as int;

    if (min > max) {
      throw DartBlockException(
        title: 'Invalid Range',
        message: 'min ($min) cannot be greater than max ($max)',
      );
    }

    final random = math.Random();
    final value = min + random.nextInt(max - min);
    return DartBlockAlgebraicExpression.fromConstant(value);
  },
  category: DartBlockNativeFunctionCategory.random,
  type: DartBlockNativeFunctionType.randomInt,
  description:
      'Generate a random integer between min (inclusive) and max (exclusive).',
);