|
I would like to use a function that returns a integer value between two values. I met the rand() function, but I need a value in a range.
[]'s
Cristiano.
|
|
|
|
|
|
Or easier,
rand () % n
If you want a range between A and B then just shift everything along:
int value = a + (rand () % (b - a));
|
|
|
|
|
This not work corretly. The returned value is zero.
[]'s
|
|
|
|
|
The following will print 100 random values between 120 and 320
int getRange (int low, int high)
{
return low + (rand () % (high - low));
}
int main (int argc, char *argv[])
{
srand (time (NULL));
for (int x = 0; x < 100; x++)
printf ("Random Value %d = %d\n", x, getRange (120, 320));
return 0;
}
|
|
|
|
|
Almost...
The problem is:
int rand(void);
so it should be:
rand()*n/RAND_MAX
or
((double)rand()/RAND_MAX)*n
Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.
|
|
|
|
|
This is not correct. The expression yields a value between 0 and n, but the probability of n is miniscule (1/(RAND_MAX +1), to be precise), which is not the probability for the other values. The following return a random value between 0 and n with uniform distribution:
int randn(int n)
{
int r;
while((r=rand())==RAND_MAX);
return ((double)r/RAND_MAX)*(n+1);
}
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
When using a variable argument list in a function is there a safe way determine how many arguments there are and what type they are before getting them off list?
Happy programming!!
|
|
|
|
|
try _vsnprintf, _vsnwprintf.
bibamus, edamus, cras moriemur [eat, drink, for tomorrow we die]
|
|
|
|
|
There isn't any way to do this. printf and routines like it are VERY dependent on the format string telling the routine exactly what to expect in the calling arguments. Thus, if your calling arguments don't match the format string, you get a crash.
In other cases, you might use a NULL value to terminate the argument list. Or you could even have a count at the start of the argument list. It is really up to you exactly what you want to do.
The only thing you have to remember is that there is no way to get the real number of arguments on the stack amd you can't tell what types of arguments were placed on the stack.
Tim Smith
"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
|
|
|
|
|
According to the spec for var args:
It is upto the calling routine to specify in some manner how many arguments there are, since it is not always possible to determine the number of arguments from the stack frame
Best way would be to pass a parameter before the var args specifying how many arguments are found or, like sprintf, just trawl through the arguments and hope they are all there. Neither are particulary robust...
|
|
|
|
|
How can I get my IP (dotted format) programmatically (I'm using Dialup Modem if this matters, <ye ye,="" don't="" laugh="">)?
--BlackSmith--
"With the help of all mighty", 2001, Me.
|
|
|
|
|
gethostname()
bibamus, edamus, cras moriemur [eat, drink, for tomorrow we die]
|
|
|
|
|
gethostname() retrieves my machine/username/(watever...)
I need my IP number (255.23.01.121...) when I'm connected to the INET,
the one appears in ipconfig command under IP Address section.
I hope my Q is clearer now .
10X.
--BlackSmith--
"With the help of all mighty", 2001, Me.
|
|
|
|
|
There may well be some nice Win32 functions, but using the Unix socket functions:
struct hostent *ent = gethostbyname (gethostname ());
struct in_addr *in = (struct in_addr *)ent->h_addr_list[0];
printf ("My host IP is %s\n", inet_ntoa (*in));
Very messy I know, and with a distinct lack of error checking
|
|
|
|
|
i don't know how to get it in c++ under windows, but i know in java it is very easy, if help, i will post code for u.
cheers
includeh10
|
|
|
|
|
Hi,
I am trying to receive a HWND_BROADCAST message within an ATL application. I have tried:
1) Adding "ON_REGISTERED_MESSAGE(WM_CFM, OnMyMessage)" to the MessageMap but that doesn't appear to work in a ATL project.
2) I have tried overriding DefWindowProc() and ProcessWindowsMessage, but they are never called.
Can anyone offer me any advice?
Cheers
Richard
|
|
|
|
|
I assume that you have called RegisterWindowsMessage in both the sending and receiving applications.
Happy programming!!
|
|
|
|
|
I saw sample code using sockets and, in the client application, before calling connect there was a bind call, apparently doing nothing.
Was it really necessary?
rechi
|
|
|
|
|
It depends. The client might have wanted to bind to a specific port for some reason. If the sockaddr structure was given a number other than 0 then the client wanted to use a specific port on its end.
bibamus, edamus, cras moriemur [eat, drink, for tomorrow we die]
|
|
|
|
|
TCP:
Server - needs a bind before listen
Client - no bind needed
UDP:
Server and Client - if You use bind You will be able to use send and recv, but You might also use sendto and recvfrom. In that case bind is not needed.
Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.
|
|
|
|
|
AAAaahhh. My mistake, sorry.
As for TCP it's all true, but as for UDP:
Client - bind is not needed and You can use connect function with send/recv or sendto/recvfrom without connect
Server - bind is needed to specify the address and port
Uh, it's been a long time since i've used sockets. Sorry again, and sorry in advance if I made any more mistakes
Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.
|
|
|
|
|
Thanx both of you for your efforts!
rechi
|
|
|
|
|
Hi,
I have a need to move a file from one folder to another. It is a .bmp file. How do I do this from within my application.
Thanks
Regards
Ruca
|
|
|
|
|
Use MoveFile
BOOL MoveFile(
LPCTSTR lpExistingFileName, // file name
LPCTSTR lpNewFileName // new file name
);
|
|
|
|