Skip to content

Commit

Permalink
Add CommonRandom class
Browse files Browse the repository at this point in the history
  • Loading branch information
saropa committed Oct 22, 2024
1 parent e232be9 commit 8c952d0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ app.*.map.json
/android/app/release
.fvm/flutter_sdk
.fvm/fvm_config.json
dartdoc-log.txt
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions lib/random/common_random.dart
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 43 additions & 0 deletions test/random/common_random_test.dart
Original file line number Diff line number Diff line change
@@ -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<void>.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);
// });
});
}

0 comments on commit 8c952d0

Please sign in to comment.