diff --git a/.gitignore b/.gitignore index f1df3ee..2cdd802 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,4 @@ app.*.map.json /android/app/release .fvm/flutter_sdk .fvm/fvm_config.json +dartdoc-log.txt diff --git a/analysis_options.yaml b/analysis_options.yaml index 539e585..7a2cbfc 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -5,3 +5,6 @@ # Ref: https://pub.dev/packages/very_good_analysis include: package:very_good_analysis/analysis_options.yaml +analyzer: + errors: + omit_local_variable_types: ignore diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml index 539e585..cfef67f 100644 --- a/example/analysis_options.yaml +++ b/example/analysis_options.yaml @@ -5,3 +5,9 @@ # Ref: https://pub.dev/packages/very_good_analysis include: package:very_good_analysis/analysis_options.yaml + +linter: + rules: + # for code clarity (speed of reading) we always want to specify types + always_specify_types: true + omit_local_variable_types: false diff --git a/lib/random/common_random.dart b/lib/random/common_random.dart new file mode 100644 index 0000000..04ea0d5 --- /dev/null +++ b/lib/random/common_random.dart @@ -0,0 +1,17 @@ +import 'dart:math'; + +/// Every time you reinstall your app, the CommonRandom() is being initialized +/// with the same starting conditions. To ensure you get a different result +/// each time, use the current timestamp as a seed. This makes the seed +/// dynamic and unique each time. +/// +/// NOTE: if you call CommonRandom() multiple times in quick succession, they +/// might generate the same value due to the seed being based on +/// [DateTime.now().millisecondsSinceEpoch] +/// +/// To get distinct values, you should initialize CommonRandom once and reuse +/// it. +/// +// ignore: non_constant_identifier_names +Random CommonRandom([int? seed]) => + Random(seed ?? DateTime.now().millisecondsSinceEpoch); diff --git a/pubspec.yaml b/pubspec.yaml index 822418a..6b093b5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: saropa_dart_utils description: "Boilerplate reduction tools and human readable extension methods by Saropa" -version: 0.2.1+Adelaide +version: 0.2.2+Pittsburgh homepage: https://app.saropa.com repository: https://github.com/saropa/saropa_dart_utils diff --git a/test/random/common_random_test.dart b/test/random/common_random_test.dart new file mode 100644 index 0000000..1f26abf --- /dev/null +++ b/test/random/common_random_test.dart @@ -0,0 +1,43 @@ +import 'dart:math'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:saropa_dart_utils/random/common_random.dart'; + +void main() { + group('CommonRandom', () { + test('unique instances with default seed', () async { + final Random random1 = CommonRandom(); + + // Add a small delay + await Future.delayed(const Duration(milliseconds: 1)); + + final Random random2 = CommonRandom(); + + // Ensure two instances are producing different values + expect(random1.nextInt(100) != random2.nextInt(100), isTrue); + }); + + test('consistent values with custom seed', () { + final Random random1 = CommonRandom(123); + final Random random2 = CommonRandom(123); + + // Ensure two instances with the same seed produce the same values + expect(random1.nextInt(100), equals(random2.nextInt(100))); + }); + + test('different values with different seeds', () { + final Random random1 = CommonRandom(123); + final Random random2 = CommonRandom(456); + + // Ensure two instances with different seeds produce different values + expect(random1.nextInt(100) != random2.nextInt(100), isTrue); + }); + + // test('handles zero seed correctly', () { + // final Random random = CommonRandom(0); + + // // Ensure instance is created and produces values + // expect(random.nextInt(100), isTrue); + // }); + }); +}