CoolRand() Function In QT/C++ [Random Numbers Generator]

Posts 1–8 of 8 · Page 1 of 1
Rand() Function In QT/C++ [Random Numbers Generator]
Mini Tut inside this tut (Tutorialception anyone?)

How to install QT for Windows:
1) Head over to http://qt.nokia.com/downloads/sdk-windows-cpp-offline
2) Once downloaded right click on installer and hit "Run As Administrator" (This is because is needs to install some things like the gcc toolchain and gcc compiler and that includes setting up system wide variables and registry edits)
3) Profit

How to install QT for Linux:
1) Head over to http://qt.nokia.com/downloads/ and choose 32bit or 64bit version (Depending on your O/S Architecture)
2) Once downloaded right click on the .run file (The installer you just downloaded) and open the permissions tab (On ubuntu anyway) then tick run as executable or whatever it says
3) Ok and close that then double click on the file and an installer should pop up follow the instructions on screen

4) Another way to install QT is via your Distro's repository which is 10^99999 times easier and allways works properly (This version is some times out dates by a few revisions which is why i put the above steps)

How to install QT for Mac:
1)I have no idea could somone please help me on this i have never used a mac or even thought of programming on it
---------------------------------------------------------------------------
Hey well this is my first tutorial so i hope its ok and its going to be a short one.

Note: This tutorial asumes that you have a basic understanding of C++ (When i say basic i mean basic such as making a new project) and QT Creator installed (This will work on VSC++ but the conversion from int to string will be different as we are using the QString libraries)

Covering how to use the Rand() function in QT C++ (IMO Greatest framework ever for XPlatform Development)

Open QT and make a new Project



Fill in the screens that proceed and click finish.

Now on the left hand side go to forms and double click on mainwindow.ui



On the left you will see a list of drag'n'dropable objects, You will need to drag 1x button and 1xLabel (Under display widgets)



Right click on your button and select "Go to Slot" once you click this a dialog window will popup, this is basicly asking when should i peform the action? and in this case we are going to use Released(), (meaning when the button is released)



Now at the top we need to include some #includes "LAAWL there was no way arround saying that"

Please add underneath what is allready there the following

Code:
#include "QString"
#include "time.h"


Now click inbetween the curly brackets of
Code:
void MainWindow::on_pushButton_released()
{

}
and put the following code wich i will explain below

Code:
    srand ( time(NULL));
    int randnumber = rand() %99 + 1;
    QString inttostr;
    inttostr.append(QString("%1").arg(randnumber));
    ui->label->setText(inttostr);


And your done now to compile and run and for the explanation of what the code does.

Now for the code explanation which i strongly reccomend you read.

srand which stands for seed random is telling our program the source in which we are generating our random number, in this case it is time
Code:
srand ( time(NULL));
This is fairly straight forward. we first create an integer called randnumber and asign it to the rand() function we then say well ok we want the range to be between 0 - 100 (99 + 1, i don't know why the hell they do it like this if someone could tell me it would be awesome) i don't normaly use %99 + 1 because it follows a patern and so i reccomend you experiment and see what works for you
Code:
int randnumber = rand() %99 + 1;
SOOOO Simple i dont need to explain but i will, this is where we create a string called inttostr
Code:
QString inttostr;
Ok so this one i feel is worth explaining so lets break it down. inttostr.append is saying were going to be editing the string inttostr,
("%1") i can't remember what this does and i am rly embaressed to admit it so if someone could tell me it would be awesome, and finally
.arg(randnumber) is basically the integer we want to convert (.arg means argument)

Code:
 inttostr.append(QString("%1").arg(randnumber));
And finally we set the label with what is contained in inttostr
Code:
ui->label->setText(inttostr);
Tip of the day: All C++ statements and lines of code must end in ; or your compiler will shit its pants

---------------------------------------------------------------------------
Please as this is my first tutorial i would love some feedback on how i can improove all my tutorials in the future
Nice. You should also edit your post and tell how to download and install QT.
Ok, i guess i can do that. thanks for the tip.
Looks cool, can you mix stdc and Cpp with qt?
stdc is standard c right? if thats what your asking it can build applications in c but no you can't mix two languages in the same programm
Quote Originally Posted by Null::Void View Post
stdc is standard c right? if thats what your asking it can build applications in c but no you can't mix two languages in the same programm
Qt is a framework that is built upon native/standard c++, so it basically just is a big amount of c++ classes put together to a framework. @.::SCHiM::. You can use native/standard c++ in any Qt application, just include the headers and you will be fine.
It's Qt, well. Many people takes QT as QuickTime. Just saying. Anyways, good tutorial.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

Talk with us