Generate random numbers in C++ WInforms

José Carlos 886 Reputation points
2022-12-29T14:57:53.507+00:00

Hi,.

I searched a lot on the Internet and I didn't find how, for example, to generate a random integer between 0 and 15 in C++ Winform. I need it

Does anyone know of a site that has a lot of information about C++ Winforms?

I have noticed that the amount of information is very limited. Find more about C# and C++ Console.

Thanks for your help always.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,080 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,709 questions
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,286 Reputation points Microsoft Vendor
    2022-12-30T01:52:10.41+00:00

    Hi @Anonymous ,

    The following code can generate random numbers in C++ Winform, you could refer to it.

      private:  
                    System::Void button1_Click(System::Object ^ sender,  
                                               System::EventArgs ^ e) {  
                      Random ^ran = gcnew Random();  
                      int RandKey = ran->Next(0, 15); //Generate random number  
                      textBox1->Text = RandKey.ToString(); //Display random number  
                    }  
    

    275011-rand.gif

    PS: This code is just for demonstration, you don't necessarily need textbox or button to trigger the event.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Castorix31 84,871 Reputation points
    2022-12-29T15:39:30.553+00:00

    It is the same thing in Winforms or Console...
    See MSDN samples in C++ : system.random

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.