Generate random numbers in Lucky7 project using C#

This is a discussion on Generate random numbers in Lucky7 project using C# within the C# Programming forums, part of the General Programming Boards category; I have problems when trying to generate random numbers in Visual C#.Net. What I am trying to do is after ...

  1. #1
    Registered User Grayson_Peddie's Avatar
    Join Date
    May 2002
    Posts
    96

    Unhappy Generate random numbers in Lucky7 project using C#

    I have problems when trying to generate random numbers in Visual C#.Net. What I am trying to do is after I declare the variable called randomslot1, randomslot2, and randomslot3 for Random, I can assign those three variables to each of 3 text box.

    Here's the code for it:

    Code:
    // C# Code:
    /// <summary>
    /// This is the code for the button. Once pressed, the button that
    /// was pressed will generate numbers to each of the text boxes.
    /// // Note: This is just a test.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void SlotSpin_Click(object sender, System.EventArgs e) {
        // A constant variable...
        const int maxnumber = 10;
        // Declare a random variable for each of the slots.
        Random randomslot1 = new Random(unchecked((int)DateTime.Now.Ticks));
        Random randomslot2 = new Random(unchecked((int)DateTime.Now.Ticks));
        Random randomslot3 = new Random(unchecked((int)DateTime.Now.Ticks));
        // Send each of those as string.
        randomslot1.ToString();
        randomslot2.ToString();
        randomslot3.ToString();
        // Send those numbers to each of the text box
        // with a maximum value of 10 each having a different value.
        Slot1.Text = (string)randomslot1.Next(maxnumber);
        Slot2.Text = (string)randomslot2.Next(maxnumber);
        Slot3.Text = (string)randomslot3.Next(maxnumber);
    }
    The compiler told me that I can't convert from int to string, and the .Net help told me that I need to provide my conversion runtime but I don't know the code for it...
    Last edited by Grayson_Peddie; 04-11-2003 at 02:45 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    // Send each of those as string.
    randomslot1.ToString();
    randomslot2.ToString();
    randomslot3.ToString();

    These lines are useless. They return a string and you don't save it anywhere. Just delete them.

    You cannot simply cast the int to string. It's no string. How about this:

    Slot1.Text = randomslot1.Next(maxnumber).ToString();
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomly generate numbers
    By maybabier in forum C Programming
    Replies: 10
    Last Post: 05-06-2009, 01:13 AM
  2. How to generate random numbers the same everytime
    By lehe in forum C++ Programming
    Replies: 11
    Last Post: 04-09-2009, 06:00 PM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 12:00 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21