17 posts

## Random Number Generator

Back to Forum: Tech Off
• Does anybody know the C# code for a console app to generate two random integers between -100 to 100, print them, print the sum of the two, and then indicate which number is bigger or if they're equal?  I'm familiar with using positivie integers, but not negative ones.  Thanks!

• rocky13 wrote:
﻿Does anybody know the C# code for a console app to generate two random integers between -100 to 100, print them, print the sum of the two, and then indicate which number is bigger or if they're equal?  I'm familiar with using positivie integers, but not negative ones.  Thanks!

Do your own homework.   You don't know the difference between negative numbers and positive numbers?

• sure. That's not too bad.

Random numbers are controlled via the class System.Random, and the .Next() property gives us a new random number.

`using System; using System.Collections.Generic; public class MyClass{    Random rand;    public static void Main()    {        rand = new Random();                int firstNumber = rand.Next(-100, 100);        int secondNumber = rand.Next(-100,100);                Console.WriteLine("{0} + {1} = {2}",             firstNumber,            secondNumber,            firstNumber + secondNumber);                    if(firstNumber > secondNumber){            Console.WriteLine("The first number {0} is bigger than the second {1}", firstNumber, secondNumber);        }else{            Console.WriteLine("The second number {0} is bigger than the first {1}", secondNumber, firstNumber);        }    }}`

• rocky13 wrote:
I'm familiar with using positivie integers, but not negative ones.

That has to win some sort of award for the most thinly disguised way of asking people to do your homework.

• evildictaitor wrote:

Random numbers are controlled via the class System.Random, and the .Next() property gives us a new random number.

`using System; using System.Collections.Generic; public class MyClass{    Random rand;    public static void Main()    {        rand = new Random();                int firstNumber = rand.Next(-100, 100);        int secondNumber = rand.Next(-100,100);                Console.WriteLine("{0} + {1} = {2}",             firstNumber,            secondNumber,            firstNumber + secondNumber);                    if(firstNumber > secondNumber){            Console.WriteLine("The first number {0} is bigger than the second {1}", firstNumber, secondNumber);        }else{            Console.WriteLine("The second number {0} is bigger than the first {1}", secondNumber, firstNumber);        }    }}`

You forgot the case where the firstNumber and secondNumber are equal (or was the omission intentional?). Since this is a homework assignment, I will refrain from posting the change necessary to include that case. I wonder if the original poster will be able to figure out how to make the change himself.

• Shining Arcanine wrote:
﻿You forgot the case where the firstNumber and secondNumber are equal (or was the omission intentional?). Since this is a homework assignment, I will refrain from posting the change necessary to include that case. I wonder if the original poster will be able to figure out how to make the change himself.

Way to go and point out the problem

I've noticed, the best way to retort to thinly-veiled homework questions is to paste some broken, uncompilable code or the correct logic, but in a totally esoteric language with an even more obscure syntax.

BrainfĂșck, anyone?

• Thanks for picking on a girl, guys!  My homework is done for your information.  I wanted to compare code and see if there's an easier method out there.  Your girlfriends would be proud...if you have one.

• rocky13 wrote:
Your girlfriends would be proud...if you have one.

I like to think of my virgin, celibate, and asexual lifestyle as a hallmark of my technical prowess

• rocky13 wrote:
﻿Thanks for picking on a girl, guys!  My homework is done for your information.  I wanted to compare code and see if there's an easier method out there.  Your girlfriends would be proud...if you have one.

Which school teaches C#? Seriously, this beats the hell out of teaching people to program in C using printf.

• Its like I've entered the into a parallel universe localized entirely to this thread..

• A minor nitpick against the posted solution: Random.Next(m, n) returns numbers ranging from m to n - 1.  Rather than stumble over that block every time I tend to write code like

digit = rand.Next(0, 9 + 1); // upper bound is exclusive

• Matthew van Eerde wrote:
﻿A minor nitpick against the posted solution: Random.Next(m, n) returns numbers ranging from m to n - 1.  Rather than stumble over that block every time I tend to write code like

digit = rand.Next(0, 9 + 1); // upper bound is exclusive

Yay for pedanticness Good call.

• W3bbo wrote:
﻿
 Shining Arcanine wrote:﻿You forgot the case where the firstNumber and secondNumber are equal (or was the omission intentional?). Since this is a homework assignment, I will refrain from posting the change necessary to include that case. I wonder if the original poster will be able to figure out how to make the change himself.

Way to go and point out the problem

I've noticed, the best way to retort to thinly-veiled homework questions is to paste some broken, uncompilable code or the correct logic, but in a totally esoteric language with an even more obscure syntax.

BrainfĂșck, anyone?

Well, if we are going to post solutions in esoteric languages with even more obscure syntaxes, I will post the solution in C:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ( void )
{

int a, b;

srand(time(NULL));

a = rand() % 201 - 100;

b = rand() % 201 - 100;

printf("a is %i and b is %i\n\n", a, b);

printf("%i + %i = %i\n\n", a, b, a + b);

if (a == b)
{

printf("a is equal to b\n\n");

}else{

printf("%i is greater than %i\n\n", (a > b) ? a : b, (a > b) ? b : a );

}

return 0;

}

To a modern computer science student who has probably never written a C program in his life, C is an esoteric language.

http://developers.slashdot.org/article.pl?sid=08/01/08/0348239

By the way, can anyone here write a solution to the original poster's homework problem in Fortran?

evildictaitor wrote:
﻿
 rocky13 wrote:﻿Thanks for picking on a girl, guys!  My homework is done for your information.  I wanted to compare code and see if there's an easier method out there.  Your girlfriends would be proud...if you have one.

Which school teaches C#? Seriously, this beats the hell out of teaching people to program in C using printf.

Well, as far as learning programming languages goes, my opinion is that C/C++ > C# > Java. Nearly all of my university's computer science department's courses are Java based, so I would welcome C# as the department's main language, but I really think everyone should learn to program in C/C++.

• Shining Arcanine wrote:

Well, as far as learning programming languages goes, my opinion is that C/C++ > C# > Java. Nearly all of my university's computer science department's courses are Java based, so I would welcome C# as the department's main language, but I really think everyone should learn to program in C/C++.

Assembly's definitely the way to go.

I actually had a class using assembly...  but this wasn't a CS class, it was an embedded microprocessors class under the computer engineering department.  HC12 assembly:  woohoo!

My school's CS department's pretty much entirely C++ based (not straight C; the introductory classes make heavy use of the ANSI C++ libraries and STL).

• evildictaitor wrote:

`using System; using System.Collections.Generic; public class MyClass{    Random rand;    public static void Main()    {        rand = new Random();                int firstNumber = rand.Next(-100, 100);        int secondNumber = rand.Next(-100,100);                Console.WriteLine("{0} + {1} = {2}",             firstNumber,            secondNumber,            firstNumber + secondNumber);                    if(firstNumber > secondNumber){            Console.WriteLine("The first number {0} is bigger than the second {1}", firstNumber, secondNumber);        }else{            Console.WriteLine("The second number {0} is bigger than the first {1}", secondNumber, firstNumber);        }    }}`

how do you embed code like that? i didnt know if there are a tag that automatically formats your code e.g: [code="c#"]?

• punkouter wrote:
how do you embed code like that? i didnt know if there are a tag that automatically formats your code e.g: [code="c#"]?

There isn't, you need to do it by hand in HTML mode.