Tech Off Thread

17 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Random Number Generator

Back to Forum: Tech Off
  • User profile image
    rocky13

    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!

  • User profile image
    cutechick22

    your gonna fail lol

  • User profile image
    RichA

    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?

  • User profile image
    evildictait​or

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

  • User profile image
    Antitorgo

    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.

  • User profile image
    Shining Arcanine

    evildictaitor wrote:
    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);        }    }}


    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.

  • User profile image
    W3bbo

    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 Wink

    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? Wink

  • User profile image
    rocky13

    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.

  • User profile image
    W3bbo

    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 Smiley

  • User profile image
    evildictait​or

    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.

  • User profile image
    stevo_

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

  • User profile image
    Matthew van Eerde

    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

  • User profile image
    evildictait​or

    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 Tongue Out Good call.

  • User profile image
    Shining Arcanine

    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 Wink

    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? Wink


    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. Smiley

    For more information on that topic, see:

    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++.

  • User profile image
    Cannot​Resolve​Symbol

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

  • User profile image
    punkouter

    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#"]?

  • User profile image
    W3bbo

    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.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.