Help me, problem with my random number generator.

Updated on May 10, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on May 9, 2017

Alright. So i want to use user input to declare from and until what numbers someone wants to practice maths with. I’ll give an example:

I first had:

Random rnd = new Random();

            int num01 = rnd.Next(1, 10);
            int num02 = rnd.Next(1, 10);

This was alright but i want people to choose their own numbers so i made this: (Its in dutch tho)

// Hier vraagt hij welke getallen je wilt gebruiken 

            Console.WriteLine(Vanaf welke nummers wil je oefenen?“);
            int num1 = Convert.ToInt32 (Console.ReadLine());

            Console.WriteLine(Tot welke nummers?“);
            int num2 = Convert.ToInt32(Console.ReadLine());

            Random rnd = new Random();

            int num01 = rnd.Next(num1, num2);
            int num02 = rnd.Next(num1, num2);

But when you try it and you type in what numbers you want it doesnt choose random numbers but prints the numbers that you typed in.

Here’s the whole script. (Still in dutch)
using System;

namespace TestApp
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            // Hier vraagt hij welke getallen je wilt gebruiken. 

            Console.WriteLine(Vanaf welke nummers wil je oefenen?);
            int num1 = Convert.ToInt32 (Console.ReadLine());

            Console.WriteLine(Tot welke nummers?);
            int num2 = Convert.ToInt32(Console.ReadLine());

            Random rnd = new Random();

            int num01 = rnd.Next(num1, num2);
            int num02 = rnd.Next(num1, num2);

            Console.Clear();
        Start:

            // De rekenmachine dingen
            Console.WriteLine(Hoeveel is  + num01 +  keer  + num02 + ?);
            int antwoord = Convert.ToInt32 (Console.ReadLine());

            if (antwoord == num01 * num02)
            {
                int reactie = rnd.Next(1, 4);

                switch (reactie)
                {
                    case 1:
                        Console.WriteLine(Goed zo, dat klopt!);
                        break;

                    case 2:
                        Console.WriteLine(Dat is waar, goed gegokt ;));
                        break;

                    case 3:
                        Console.WriteLine(Goed gedaan!);
                        break;

                    case 4:
                        Console.WriteLine(Lekker bezig!);
                        break;
                }
            }
            else
            {
                // Hij reageert gebaseerd op hoe dichtbij je was.
                int diff = Math.Abs(antwoord  (num01 * num02));

                if (diff == 1)
                {
                    Console.WriteLine(Bijna!);
                }
                else if (diff <= 10)
                {
                    Console.WriteLine(Je kan het wel, nog even oefenen!);
                }
                else {
                    Console.WriteLine(Je komt niet eens dichtbij...);
                }

            }

            //Hier gaat hij terug naar start.
            Console.WriteLine();
            Console.WriteLine(Druk op een knop om nog een som op te lossen.);
            Console.ReadKey();
            Console.WriteLine();
            Console.Clear();
            goto Start;
        }    
    }
}

  • Liked by
Reply
0 on May 9, 2017

Alright, so i think i figured it out, it gives an error that min01 cant be bigger then min02. How do i fix that? Im such a code noob.

  • Liked by
Reply
Cancel
0 on May 9, 2017

Ok your problem is the way you save the numbers. You are using the same integers to define the size of the random number as you use to save the random number. Change that, use a new integer to save the random numbers you create.

Other than that your program has no exit condition. Which means if you were to build and compile it and give out the user wouldn’t be able to leave your program and would have to hard kill it. Now that is not really a problem since a lot of programs don’t have that (look at your browser) but I think it is a good habit to develop. Give the user an option to leave the program.

Also gotos make your code really confuding once it get’s larger so in general it would be good to avoid them. Here you could easily use a while loop which would also allow you to create an exit condition.

  • Liked by
Reply
Cancel
0 on May 10, 2017

The GoTo is just so it loops, this code is just an example because i’ll be writing an application with it.

I still don’t get it tho.

It asks for num1, the first one,
then it asks for num2,
qThe GoTo is just so it loops, this code is just an example because i’ll be writing an application with it.

I still don’t get it tho.

It asks for num1, the first one,
then it asks for num2,
    
then it uses it like this:
 int num01 = rnd.Next(num1, num2);            
 int num02 = rnd.Next(num1, num2);

so it uses new integers. Help me!

  • Liked by
Reply
Cancel
1 on May 10, 2017

Oh I didn’t see that it was actually num1, num2 and num01 and num02, since the code has some weird bright color.

I get that the goto is for the loop I would just recommend to use a while loop for the loop instead.

So I decided to try to run your code and it runs just fine for me. I entered the numbers 1 and 99 and got 32 and 65 in the math question.

Also since I am on an ubuntu I have to force close my terminal to leave your program. Which makes me angry because I can’t use it for something else afterwards 😛

on May 10, 2017

Runs just fine for me. I entered the numbers 1 and 99 and got 32 and 65 in the math question.
From InSicK

It didn’t change the numbers after that.

Show more replies
  • Liked by
Reply
Cancel
2 on May 10, 2017

Oh, lol. I’m on mac.

I figured it out: the Start: was placed too early, it wasn’t generating new numbers but it declared it the first time.

it was like this:

    Console.WriteLine(“Vanaf welke nummers wil je oefenen?“);
            int num1 = Convert.ToInt32 (Console.ReadLine());

            Console.WriteLine(“Tot welke nummers?“);
            int num2 = Convert.ToInt32(Console.ReadLine());

            Random rnd = new Random();

            int num01 = rnd.Next(num1, num2);
            int num02 = rnd.Next(num1, num2);

            Console.Clear();
        Start:

That’s wrong, since it doesnt generate new numbers. The fix:

Console.WriteLine(“Vanaf welke nummers wil je oefenen?”);
            int vrgnmr1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(“Tot welke nummers?”);
            int vrgnmr2 = Convert.ToInt32(Console.ReadLine());
            
            Start:

            Random rnd = new Random();

            int num01 = rnd.Next(vrgnmr1, vrgnmr2);
            int num02 = rnd.Next(vrgnmr1, vrgnmr2);
            Console.Clear();

How do i implement while in the code?

Helpful
on May 10, 2017

What do you mean? You comment out/delete your old code and put in the new one.

on May 10, 2017

The start from the goto was in the wrong place, it made a random nummer the first time but not afterwards since it didn’t run again.

Show more replies
  • Liked by
Reply
Cancel