Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]How to generate random number between two numbers? Qt
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to generate random number between two numbers? Qt

Scheduled Pinned Locked Moved General and Desktop
10 Posts 8 Posters 69.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zain
    wrote on 28 Jan 2013, 07:37 last edited by
    #1

    Hello

    Two numbers are taken as inputs & now I want to generate random number between them.

    Please tell me how to do it?

    Thanks
    Zain

    1 Reply Last reply
    1
    • H Offline
      H Offline
      hpollak
      wrote on 28 Jan 2013, 08:00 last edited by
      #2

      You can use the cstdlib.

      See: "cplusplus-doc":http://www.cplusplus.com/reference/cstdlib/rand/

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on 28 Jan 2013, 08:08 last edited by
        #3

        Did u tried using qrand() ??

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bambo
          wrote on 28 Jan 2013, 08:13 last edited by
          #4

          random number from interval <a,b>: @x = a + rand() * (b-a)@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Macro
            wrote on 28 Jan 2013, 08:19 last edited by
            #5

            Example:

            MainWindow.h
            @
            #include <QMainWindow>
            class MainWindow : public QMainWindow
            {
            Q_OBJECT

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

            private slots:
            void on_pushButton_clicked();

            private:
            Ui::MainWindow *ui;
            int High;
            int Low;
            };
            @

            MainWindow.cpp

            @
            #include "MainWindow.h"
            #include "ui_MainWindow.h"
            #include <qglobal.h>

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
            {
            ui->setupUi(this);
            High = 10;
            Low = 5;
            }

            MainWindow::~MainWindow()
            {
            delete ui;
            }

            void MainWindow::on_pushButton_clicked()
            {
            qsrand(qrand());
            ui->lineEdit->setText(QString::number(qrand() % ((High + 1) - Low) + Low));
            }
            @

            1 Reply Last reply
            1
            • Z Offline
              Z Offline
              Zain
              wrote on 28 Jan 2013, 08:23 last edited by
              #6

              Thank u all

              Done with it.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on 28 Jan 2013, 10:34 last edited by
                #7

                Note that qrand produces pseudo-random number only. You do want to use something better for e.g. crypto operations.

                1 Reply Last reply
                1
                • T Offline
                  T Offline
                  Todd987
                  Banned
                  wrote on 10 Apr 2018, 10:01 last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rsarov
                    wrote on 11 Jul 2019, 09:02 last edited by rsarov 7 Nov 2019, 09:03
                    #9

                    modern Qt c++ 11

                    #include <random>
                    #include "QDateTime"
                    
                    int getRand(int min, int max){
                        unsigned int ms = static_cast<unsigned>(QDateTime::currentMSecsSinceEpoch());
                        std::mt19937 gen(ms);
                        std::uniform_int_distribution<> uid(min, max);    
                        return uid(gen);
                    }
                    
                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      Vadi2
                      wrote on 20 Oct 2019, 08:40 last edited by
                      #10

                      Or modern Qt: https://doc.qt.io/qt-5/qrandomgenerator.html :)

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved