Random double generation between a range in dart - flutter

Need to generate random doubles between a range
The nextint() function takes a param max where as the nextdouble() doesnt take any params.
Is there any other methods that return random doubles between a range in dart?

The nextDouble returns a value between 0 and 1 (not included). So, if you want a number in the range a (included) to b (not included), you can just do:
double doubleInRange(Random source, num start, num end) =>
source.nextDouble() * (end - start) + start;
print(doubleInRange(random, a, b));

No, there isn't, but it easy to recreate it since nextInt takes only a max value (exclusive).
nextDouble() * max

I doubt there is. If you want just double values you can convert the integer value to double
import 'dart:math';
main() {
var rng = new Random();
for (var i = 0; i < 10; i++) {
print(rng.nextInt(100).toDouble());
}
}
If you want the type of double values generated by nextDouble() such as '0.2502033576383784' i suggest you create a function to handle the range of values.
import 'dart:math';
main() {
var rng = new Random();
for (var i = 0; i < 10; i++) {
print(rng.nextDouble() + rng.nextInt(50));
}
}

I have one more simple solution to add to the list(only with Max range).
var random = Random();
int randomInt = random.nextInt(45);
double randTemp = random.nextDouble() * randomInt;

Related

flutter - return/show plus sign of int

I'm trying to build some training app for functions for school. The problem is:
Everytime the randomly picked number is lower than 0, my function shows +-, because
I have a fixed format for my function.
EXAMPLE
I tried to use the NumberFormat of the Intl-Package, but then I can't use the int-values correctly. Is there a way to show a plus sign for positive numbers, while they are still usable to work with them?
Code so far:
int randomNumberMinMax(int min, int max){
int randomminmax = min + Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min + Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x+$b)²+ $c";
You could only show the plus when the number is positive like this for example
String task = "f(x) = $a(x${b >= 0 ? "+" : ""}$b)²${c >= 0 ? "+" : ""} $c";

Flutter find the sum of digits of int value

I want to find the sum of the digits of the number entered in Flutter. I want to encode this algorithm.
for example
x=1992
result=1+9+9+2=21
how can i do this with flutter
You can do in this way.
import 'dart:io';
void main() {
print('Enter X');
int X = int.parse(stdin.readLineSync()!);
int result = 0;
for (int i = X; i > 0; i = (i / 10).floor()) {
result += (i % 10);
}
print('Sum of digits\n$result');
}
Output
Enter X
123456
Sum of digits
21
transform the number into an String using String stringValue = x.toString();
create an array from each char using List<String> result = stringValue.split('');
sum each number transforming back using int.parse(result)
void main(){
int x = 1992;
String stringValue = x.toString();
List<String> result = stringValue.split('');
int sum = 0;
for(int i = 0 ; i < result.length; i++) {
int value = int.parse(result[i]);
sum = sum + value;
}
print(sum);
}
Result: 21

How to create a random list of doubles (Flutter)?

List<AudioWaveBar> bars = [];
var list = List<double>.generate(100, (i) => i as double)..shuffle();
for (var i = 0; i <= list.length; i++) {
bars.add(AudioWaveBar(
heightFactor: list[i],
color: widget.podcast.percentPlayed >= ((i + 1).toDouble() / list.length.toDouble())
? AppTheme.greenStart
: Colors.white10
)); }
I need to have a list of random doubles only (from 0 to 1), but random function doesn't add the numbers to a list and generate function only accepts int numbers that can't be casted. Any help? thanks!
The function Random().nextDouble() generates a random double between 0 and 1.
So just by specifying a max value u can get what u want, like so :
int maxValue = 1000;
var list = List<double>.generate(100, (i) => Random().nextDouble() * maxValue)..shuffle();

How to Double Parse A String and Round It to 2 Decimal Places

I want the String values that are added from
List<String> priceStringList
and converted into Doubles to
List<double> doubleList
to only have 2 decimal places.
for (int j = 0; j < priceStringList.length; j++) {
var doubleData = double.parse(priceStringList[j]);
doubleList.add(doubleData);
}
Sample data for doubleList after the for loop include:
[3.429, 3.44, 3.49, 3.41683, 3.35501, 3.02, 3.06, 3.17947,...
But I want the values to only include the first 2 digits after the decimal point. I tried using
var doubleData = Math.Round(double.parse(priceStringList[j]), 2);
but I get an error stating "Undefined name 'Math'".
EDIT I have already added import 'dart:math';
Try this:
double roundDouble(double value, int places){
double mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
var doubleData = roundDouble(double.parse(priceStringList[j]), 2);
To call Math you have to import it as Math, for example:
import 'dart:math' as Math;

How to count digits in BigDecimal?

I’m dealing with BigDecimal in Java and I need to make 2 check against BigDecimal fields in my DTO:
Number of digits of full part (before point) < 15
Total number of
digits < 32 including scale (zeros after point)
What is the best way to implement it? I extremely don’t want toBigInteger().toString() and .toString()
I think this will work.
BigDecimal d = new BigDecimal("921229392299229.2922929292920000");
int fractionCount = d.scale();
System.out.println(fractionCount);
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
System.out.println(wholeCount);
I did some testing of the above method vs using indexOf and subtracting lengths of strings. The above seems to be signficantly faster if my testing methodology is reasonable. Here is how I tested it.
Random r = new Random(29);
int nRuns = 1_000_000;
// create a list of 1 million BigDecimals
List<BigDecimal> testData = new ArrayList<>();
for (int j = 0; j < nRuns; j++) {
String wholePart = r.ints(r.nextInt(15) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
String fractionalPart = r.ints(r.nextInt(31) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
BigDecimal d = new BigDecimal(wholePart + "." + fractionalPart);
testData.add(d);
}
long start = System.nanoTime();
// Using math
for (BigDecimal d : testData) {
int fractionCount = d.scale();
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
}
long time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
start = System.nanoTime();
//Using strings
for (BigDecimal d : testData) {
String sd = d.toPlainString();
int n = sd.indexOf(".");
int m = sd.length() - n - 1;
}
time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
}