3

I'm trying to generate two different random numbers and add those together, but Flutter doesn't seem to like my math. I keep getting the message that '+' isn't defined for the class Random.

import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(MaterialApp(
      title: 'Random Numbers',
      theme: ThemeData(primarySwatch: Colors.orange),
      home: MyHome(),
    ));

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {
    var num1 = new Random();
    for (var i = 0; i < 10; i++) {
      print(num1.nextInt(10));
    }
    var num2 = new Random();
    for (var i = 0; i < 10; i++) {
      print(num2.nextInt(10));
    }
    //var sum = num1 + num2;

    return Container();
  }
}

My goal is to display it something like this: "2 + 5 = " where the user will fill in the answer. If correct do this else do that.

George
  • 6,886
  • 3
  • 44
  • 56
J.W
  • 1,135
  • 4
  • 13
  • 21

2 Answers2

3

The error is telling you that you're trying to add two Random objects, and not two numbers. You're printing them correctly, using nextInt() on your loops, but when you try to sum them, you're using the original variable of the type Random.

Try this:

class _MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {

    // Instantiate a Random class object
    var numGenerator = new Random();

    //You don't need a second loop because it was the same exact code,
    //only with a different variable name.
    for (var i = 0; i < 10; i++) {
      print(numGenerator.nextInt(10));
    }
    // Save the numbers you generated. Each call to nextInt returns a new one
    var num1 = numGenerator.nextInt(10);
    var num2 = numGenerator.nextInt(10);
    var sum = num1 + num2;

    //use num1, num2 and sum as you like    

    return Container();
  }
}
George
  • 6,886
  • 3
  • 44
  • 56
  • Thank you very much. Now when I run it I got 10 ints from each num1 and num 2. From the sum I got one answer, which doesn't sum up with num1 and num2. I/flutter (17272): num 1 I/flutter (17272): 2 I/flutter (17272): num 2 I/flutter (17272): 3 I/flutter (17272): sum I/flutter (17272): 12 – J.W Jan 29 '19 at 15:11
  • I ran it with just one calculation: Num1 =>2; Num2 =>3; sum=>12 – J.W Jan 29 '19 at 15:17
  • @J.W everytime you call `nextInt` you get a new number. Are you saving those numbers? Maybe we should make the code a bit cleaner, I've edited the answer to make it better. Save the value you generated so you can know later what that number was. – George Jan 29 '19 at 19:33
  • @J.W If this solved your problem, please mark this answer as correct by clicking the green check under the voting section on my answer. Thanks :) – George Feb 01 '19 at 11:24
0

Thank you very much Gerorge and Sorry for my abscense.

I got some help to solve this through dart

Random seed = Random();
const int MAX_VALUE = 10;

int val1 = seed.nextInt(MAX_VALUE);
int val2 = seed.nextInt(MAX_VALUE);
int sum = val1 + val2;

print('$val1 + $val2 = $sum'); 
J.W
  • 1,135
  • 4
  • 13
  • 21