I want to divide adynamic List int five equal parts - flutter

I want to divide a dynamic list into five equal parts and take out one random number from each part. In doing so, I want to avoid duplicate numbers. Here is my code and it was not good at all.
List document;
int value1 = Random().nextInt((document.length / 5).round());
int value2 = Random().nextInt((document.length / 5).round()) +
((document.length / 5).round()) + 1;
int value3 = Random().nextInt((document.length / 5).round()) +
((document.length / 5).round() * 2) + 1;
int value4 = Random().nextInt((document.length / 5).round()) +
((document.length / 5).round() * 3) + 1;
int value5 = Random().nextInt((document.length / 5).round()) +
((document.length / 5).round() * 4) + 1;`
If you have a better way, I would appreciate it.

You can get this with a simple while loop and check if the element exists in the list already like the following
import "dart:math";
void main() {
List<int> intList = [];
List<int> randomList = [];
for (int i = 0; i < 100; i++) {
intList.add(i);
}
while(randomList.length < 5){
int _randomNum = Random().nextInt(intList.length);
if(randomList.contains(_randomNum) == false)
{
randomList.add(_randomNum);
}
}
print(randomList);
}

Related

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

Flutter: How to force randomly generated value to be higher than the second one Dart

So I have this function where the system generates random numbers for an arithmetic quiz app. Addition and Multiplication work perfectly. But since it is for kids between the age of 5 to 10, I do not want Subtraction to generate negative answers and Division needs to generate whole numbers.
Currently the subtration generates lower number than the second number which gives negative answer.
Please help on how to force the randomly generated number to be higher than the second value.
List<dynamic> questions = [];
List<dynamic> answers = [];
bool isMarked = false;
List<List<dynamic>> mcq = [];
List<dynamic> userAnswer = [];
List<dynamic> ansData = [];
List<dynamic> ans = [];
var j = 0;
final CountDownController _controller = CountDownController();
#override
void initState() {
super.initState();
for (var i = 1; i < int.parse(widget.numOfQuestions) + 1; i++) {
ans = [];
final val1 = Random().nextInt(int.parse(widget.range1)) + 1;
final val2 = Random().nextInt(int.parse(widget.range2)) + 1;
if (widget.operator == 'sum') {
questions.add('$val1 + $val2 = ? ');
answers.add(val1 + val2);
ansData = [
val1 + val2,
val1 + val2 + Random().nextInt(10) + 1,
val1 + val2 - Random().nextInt(10) - 1,
val1 + val2 + Random().nextInt(16) + 1,
];
} else if (widget.operator == 'minus') {
questions.add('$val1 - $val2 = ? ');
answers.add(val1 - val2);
ansData = [
val1 - val2,
val1 - val2 + Random().nextInt(10) + 1,
val1 - val2 - Random().nextInt(10) - 1,
val1 - val2 + Random().nextInt(16) + 1,
];
} else if (widget.operator == 'multiplication') {
questions.add('$val1 * $val2 = ? ');
answers.add(val1 * val2);
ansData = [
val1 * val2,
val1 * val2 + Random().nextInt(10) + 1,
val1 * val2 - Random().nextInt(10) - 1,
val1 * val2 + Random().nextInt(20) + 1,
];
} else {
questions.add('$val1 / $val2 = ? ');
answers.add((val1 / val2).toStringAsFixed(2));
ansData = [
(val1 / val2).toStringAsFixed(2),
(val1 / val2 + Random().nextInt(10) + 1).toStringAsFixed(2),
(val1 / val2 - Random().nextInt(10) - 1).toStringAsFixed(2),
(val1 / val2 + Random().nextInt(16) + 1).toStringAsFixed(2),
];
}
for (var j = 0; j < 4; j++) {
final rNum = Random().nextInt(ansData.length).round();
ans.add(ansData[rNum]);
ansData.removeAt(rNum);
}
mcq.add(ans);
}
}
You could do
final val2 = val1 + Random().nextInt(int.parse(widget.range2) - val1) + 1;

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.);
}

How to convert a binary number into a decimal fraction in dart?

Hi i have been wondering if there is a way in which to convert binary numbers into decimal fractions.
I know how to change base as an example through this code
String binary = "11110010";
//I'd like to change this line so it produces a decimal value
String denary = int.parse(binary, radix: 2).toRadixString(10);
If anyone still wondering how to convert decimal to binary and the inverse:
print(55.toRadixString(2)); // Outputs 110111
print(int.parse("110111", radix: 2)); Outputs 55
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
This is my c++ solution but you can implement any language

Memory Access Exceedingly Slow For a Simple Loop Through an Array

I am taking about 50 times as long as expected to loop through a simple assignment. My first reaction was that I had disordered my memory access in the arrays, resulting in cache misses. This doesn't seem the case, however.
The pixel value assignment and updating the arrays takes a dogs age. Do any one of you folks have an inclining as to why this is happening? (I am compiling for an iPod with an A4)
memset(columnSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageWidth/numSubdivisions) );
memset(rowSumsCurrentFrameA, 0, sizeof(unsigned int) * (_validImageHeight/numSubdivisions) );
int pixelValue = 0;
int startingRow = 0;
int startingColumn = 0;
for (int i = 0; i < _validImageHeight/numSubdivisions; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < (_validImageWidth/numSubdivisions); j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}
The result of _validImageWidth/numSubdivisions must be an integer, are you sure that is always the case?
Also, you should calculate _validImageWidth/numSubdivisions before entering the double loops, it's not safe to assume your compiler takes care of it.
int limit = _validImageHeight/numSubdivisions;
for (int i = 0; i < limit; i++)
{
int index = (i + startingRow) * _imageWidth;
for( int j = 0; j < limit; j++)
{
pixelValue = imageData[index + startingColumn + j];
columnSumsCurrentFrameA[j] += pixelValue;
rowSumsCurrentFrameA[i] += pixelValue;
}
}