Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. ZMQ / random generator
Forum Updated to NodeBB v4.3 + New Features

ZMQ / random generator

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 4 Posters 459 Views 1 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.
  • E Offline
    E Offline
    ELIF
    wrote on last edited by
    #1

    I use ZMQ in QT .In server code :

     srand (time(NULL));
    while (1) {
        char buffer [100];
        for(int i=0; i<100; i++){
            buffer[i]=rand()%1000;
        }
    zmq_recv (responder, buffer, 100, 0);
    
     In client code QT:
    
          ```
        zmq_recv (requester, buffer, 100, 0);
        for(int i=0; i<100; i++){
        printf("%c : ",buffer[i]);
        qDebug() << "buffer[" << i << "] : " << buffer[i];
        }
                   ```
    

    But I get negative values also.I want to get values between 0-1000 .What is the problem?
    My output is

    ...
    buffer[ 44 ] :  -1476324256
    buffer[ 45 ] :  127
    buffer[ 46 ] :  0
    buffer[ 47 ] :  0
    buffer[ 48 ] :  0
    buffer[ 49 ] :  0
    buffer[ 50 ] :  0
    buffer[ 51 ] :  0
    buffer[ 52 ] :  -1717986918
    buffer[ 53 ] :  1069128089
    buffer[ 54 ] :  -1301787792
    buffer[ 55 ] :  85
    buffer[ 56 ] :  0
    buffer[ 57 ] :  0
    buffer[ 58 ] :  -1288257088
    buffer[ 59 ] :  127
    buffer[ 60 ] :  0
    buffer[ 61 ] :  0
    buffer[ 62 ] :  0
    buffer[ 63 ] :  0
    buffer[ 64 ] :  0
    buffer[ 65 ] :  1072693248
    ....
    
    JonBJ 1 Reply Last reply
    0
    • E ELIF

      I use ZMQ in QT .In server code :

       srand (time(NULL));
      while (1) {
          char buffer [100];
          for(int i=0; i<100; i++){
              buffer[i]=rand()%1000;
          }
      zmq_recv (responder, buffer, 100, 0);
      
       In client code QT:
      
            ```
          zmq_recv (requester, buffer, 100, 0);
          for(int i=0; i<100; i++){
          printf("%c : ",buffer[i]);
          qDebug() << "buffer[" << i << "] : " << buffer[i];
          }
                     ```
      

      But I get negative values also.I want to get values between 0-1000 .What is the problem?
      My output is

      ...
      buffer[ 44 ] :  -1476324256
      buffer[ 45 ] :  127
      buffer[ 46 ] :  0
      buffer[ 47 ] :  0
      buffer[ 48 ] :  0
      buffer[ 49 ] :  0
      buffer[ 50 ] :  0
      buffer[ 51 ] :  0
      buffer[ 52 ] :  -1717986918
      buffer[ 53 ] :  1069128089
      buffer[ 54 ] :  -1301787792
      buffer[ 55 ] :  85
      buffer[ 56 ] :  0
      buffer[ 57 ] :  0
      buffer[ 58 ] :  -1288257088
      buffer[ 59 ] :  127
      buffer[ 60 ] :  0
      buffer[ 61 ] :  0
      buffer[ 62 ] :  0
      buffer[ 63 ] :  0
      buffer[ 64 ] :  0
      buffer[ 65 ] :  1072693248
      ....
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ELIF said in ZMQ / random generator:

      But I get negative values also.I want to get values between 0-1000 .What is the problem?

      You store your random numbers in a char. That is signed, hence negative values (where value > 127). How do you intend numbers up to 1000 to fit into a char, I'd love to know?

      E 1 Reply Last reply
      3
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        you can do a simple linear projection

        something like this:

        template<typename typeIn, typename typeOut>
        typeOut mapToRange(typeIn value,
                            typeIn inMax = std::numeric_limits<typeIn>::max(),
                            typeIn inMin = std::numeric_limits<typeIn>::min(),
                            typeOut outMax = std::numeric_limits<typeOut>::max(),
                            typeOut outMin = std::numeric_limits<typeOut>::min() ){
            //Mapping follows: Y = (X-minRangeOld)/(maxRangeOld-minRangeOld) * (maxRangeNew-minRangeNew) + minRangeNew
            auto mapped = static_cast<double>(value - inMin) / (inMax - inMin) * (outMax - outMin) +  outMin;
        
            return static_cast<typeOut>(mapped);
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • JonBJ JonB

          @ELIF said in ZMQ / random generator:

          But I get negative values also.I want to get values between 0-1000 .What is the problem?

          You store your random numbers in a char. That is signed, hence negative values (where value > 127). How do you intend numbers up to 1000 to fit into a char, I'd love to know?

          E Offline
          E Offline
          ELIF
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #5

            Hi,

            Since you are using ZMQ, you should build a proper message to transmit your data.

            Don't send your int values as char. You need to convert the values into their "string" representation. Essentially your table should be ["0", "420", "12", ...].

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

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