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. I want to generate random number between the 0 to 1. with the interval of 1 second in qt.
Forum Update on Monday, May 27th 2025

I want to generate random number between the 0 to 1. with the interval of 1 second in qt.

Scheduled Pinned Locked Moved Unsolved General and Desktop
delaywaitsleep
6 Posts 5 Posters 1.1k Views
  • 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.
  • D Offline
    D Offline
    dev_liya
    wrote on 27 Jan 2022, 11:41 last edited by
    #1

    I want to generate the random value from 0 to 1 with interval of one second. i have write this code but i cannot understand how can i take delay of 1 second.

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QRandomGenerator>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->setStyleSheet("background-color: black;");
        ui->random_label->setAlignment(Qt::AlignCenter);
        ui->random_label->setStyleSheet("color: rgb(255, 255, 255);");
        
        while(true)
    {
        double value = QRandomGenerator::global()->generateDouble();
        qDebug()<<QString::number(value,'f',2);
        //i need delay here of 1 second
    }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    i need deley of 1 second after this line qDebug()<<QString::number(value,'f',2); in for loop and i also write the comment where i need delay of 1 second. please help i am new to Qt. how can i take delay of 1 second ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 27 Jan 2022, 11:46 last edited by
      #2

      Use a QTimer to execute a slot every second.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply 27 Jan 2022, 12:00
      3
      • C Christian Ehrlicher
        27 Jan 2022, 11:46

        Use a QTimer to execute a slot every second.

        D Offline
        D Offline
        dev_liya
        wrote on 27 Jan 2022, 12:00 last edited by
        #3

        @Christian-Ehrlicher but my requirement is to generate in infinite loop ad show in window.

        J S 2 Replies Last reply 27 Jan 2022, 12:01
        0
        • D dev_liya
          27 Jan 2022, 12:00

          @Christian-Ehrlicher but my requirement is to generate in infinite loop ad show in window.

          J Offline
          J Offline
          JonB
          wrote on 27 Jan 2022, 12:01 last edited by JonB
          #4

          @dev_liya
          You do not need any loop. QTimer can be set to fire every second for you, repeatedly. Put a slot on that to pick a random number and display it. No "loop" nor "delay" required. Get rid of any while(true). Trust us :)

          P 1 Reply Last reply 27 Jan 2022, 13:21
          4
          • J JonB
            27 Jan 2022, 12:01

            @dev_liya
            You do not need any loop. QTimer can be set to fire every second for you, repeatedly. Put a slot on that to pick a random number and display it. No "loop" nor "delay" required. Get rid of any while(true). Trust us :)

            P Online
            P Online
            Pl45m4
            wrote on 27 Jan 2022, 13:21 last edited by Pl45m4
            #5

            @JonB said in I want to generate random number between the 0 to 1. with the interval of 1 second in qt.:

            Trust us :)

            Trust-Rule #1: Never trust someone on the internet who said "Trust me" XD
            ... unless it's an African prince who want to send you money or you've won the lottery :o)
            [Edit: No, kids... dont do that :) ]


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            2
            • D dev_liya
              27 Jan 2022, 12:00

              @Christian-Ehrlicher but my requirement is to generate in infinite loop ad show in window.

              S Offline
              S Offline
              SimonSchroeder
              wrote on 28 Jan 2022, 07:40 last edited by
              #6

              @dev_liya said in I want to generate random number between the 0 to 1. with the interval of 1 second in qt.:

              but my requirement is to generate in infinite loop ad show in window.

              If you do an infinite loop then the event loop will not run. That means that setting the random_label or anything like that will not show on the screen as the slots cannot be handled: your infinite loop will block the event loop. The effect is that you have a frozen application. So, trust @JonB.

              As per your request: to wait for one second and block everything (you have been warned!): use QThread::sleep(1);

              1 Reply Last reply
              3

              4/6

              27 Jan 2022, 12:01

              • Login

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