-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,4 @@ app.*.map.json | |
/android/app/release | ||
.fvm/flutter_sdk | ||
.fvm/fvm_config.json | ||
dartdoc-log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
}); | ||
} |