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();
Related
Is there a better way to calculate a moving sum of a list?
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
int i = 0;
int maxLength = data.length - window + 1;
while (i < maxLength) {
List tmpData = data.getRange(i, i + window).toList();
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
i++;
}
// filling the first n values with null
i = 0;
while (i < window - 1) {
sum.insert(0, null);
i++;
}
return sum;
}
Well, the code is already clean for what you need. Maybe just some improvements like:
Use a for loop
You can use the method sublist which creates a "view" of a list, which is more efficient
To insert some values in the left/right of a list, there is a specific Dart method called padLeft, where you specify the lenght of the list which you want it to become (first parameter), then the value you want to use to fill it (second parameter). For example, if you have an array of N elements, and you want to fill it with X "null"s to the left, use padLeft(N+X, null).
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
for (int i = 0; i < data.length - window + 1; i++) {
List tmpData = data.sublist(i, i + window);
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
}
sum.padLeft(window - 1, null);
return sum;
}
if I understand your problem correctly you can just calculate the window one time and in one loop you can for each iteration you can add the current element to the sum and subtract i - (window - 1)
so for an input like this
data = [1,2,3,4,5,6]
window = 3
the below code will result in [6,9,12,15]
int sum = 0;
List<double> res = [];
for (int i = 0;i<data.length;i++) {
sum += data[i];
if (i < window - 1) {
continue;
}
res.add(sum);
sum -= data[i - (window - 1)]; // remove element that got out of the window size
}
this way you won't have to use getRange nor sublist nor reduce as all of those are expensive functions in terms of time and space complexity
I have two different list values i want get the corresponding one list between value to other list between values. Please look at the list below. Here i have 1st list value of 11 and 15 here i need to find the between values of 12, 13 and 15.
11 value =22
15 value =30
List<double> list1 =[11, 15];
List<double> list2 =[22, 30];
If I understand correct you need the interpolation function to generate values between 2 points:
List<double> interpolate(double start, double end, int count) {
if (count < 2) {
throw Exception("interpolate: illegal count!");
}
final array = List.generate(count + 1, (index) => 0.0);
for (int i = 0; i <= count; ++i) {
array[i] = start + i * (end - start) / count;
}
return array;
}
Usage:
void main() {
final input = [1.0, 15.0];
print(interpolate(input.first, input.last, 4));
}
Result:
[1.0, 4.5, 8.0, 11.5, 15.0]
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
I need to increment number dynamically in Roman
You can increment by int and convert it to number in roman by this library :
numerus
final n = 2;
print(n.toRomanNumeralString());
You could write it yourself as an exercise, or you can use a package like numerus to do this job for you.
From how do I make an integer to roman algorithm in dart?, you can get the following:
const List<int> arabianRomanNumbers = [
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
];
final builder = StringBuffer();
for (var a = 0; a < arabianRomanNumbers.length; a++) {
final times = (num / arabianRomanNumbers[a]).truncate(); // equals 1 only when arabianRomanNumbers[a] = num
// executes n times where n is the number of times you have to add
// the current roman number value to reach current num.
builder.write(romanNumbers[a] * times);
num -= times * arabianRomanNumbers[a]; // subtract previous roman number value from num
}
return builder.toString();
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;