ZMQ / random generator
Unsolved
Mobile and Embedded
-
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 ....
-
@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 achar
, I'd love to know? -
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); }
-
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", ...]
.