|
One way to obtain an array of random numbers with none repeating is as follows:
Since your random number is to be in the range 1-n, it is better to simply initialize an array of size n with the numbers 1-n:
for (int i = 0; i < size; i++)<br />
layer_array[i] = i + 1;
(BTW for peformance, don't initialize the array first with "{0}" since you are immediately initializing it one line later.)
Now, seed the random number and then go through the array again exchanging every value with the value at a randomly selected offset:
for (i = 0; i < size; i++)<br />
{<br />
int dst = rand() % size;<br />
<br />
int tmp = layer_array[i];<br />
layer_array[i] = layer_array[dst];<br />
layer_array[dst] = tmp;<br />
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Hi
I'm not sure what u mean:
(1) to fill an array with non-repeating random numbers
or (2) to shuffle array elements
From your code it looks more like 2. It also looks like the [Delete] was pushed here and there...
The technique of marking used elements is not good. If u had an array of 1M the last iteration would lots of time coz u'd repeat it lots of time till u hit the one out million left..
What u should do is this:
<br />
int i, new_array[size], old_array[size];<br />
<br />
for(i=0; i<size; i++)
init(old_array+i);<br />
<br />
for(i=0; size>0; i++) {<br />
new_array[i] = old_array[x=(rand()%size)];<br />
<br />
old_array[x] = old_array[--size];<br />
}<br />
HTH
Yakov
-- modified at 10:07 Thursday 27th October, 2005
|
|
|
|
|
I am new to the use of sockets, ports and client/server applications.
I have an application that will Connect to a proxy server (to allow for remote commands).
However if the server is temporarily lost (unexpectedly) how do I detect it returning?
My application continues to run but never re-Connects to the server.
Does the server notify me? if so how?
Should I use GetPeerName or can I continually try to Connect to an already connected port
I have a 2 minute timeout timer that I intend to use as a Connect retry control but I need to know when to start.
I am using Visual Studio 6 and VC++
|
|
|
|
|
cgb143 wrote: However if the server is temporarily lost (unexpectedly) how do I detect it returning?
If a socket connection is closed, you need to reinitiate it. On TCP/IP level there is no notification when another peer is back "online" after the connection is closed, you could try to reconnect in intervalls or work out a protocol where the server activly sends something (like a broadcast inside a LAN). Make sure you don't flood the network with wild/endless reconnect attempts, e.g. give up after the 10th reconnect attempt. Hope it helps.
|
|
|
|
|
i'm trying to implement a button that functions for example the way that the seek or pitchbend controls function on a cd player. that is, you press/'push' it and 'work starts getting done' and 'stops getting done' when/after you release the button.
the only way that i know and that works is to check mouse coordinates upon WM_LBUTTONDOWN/UP (since WM_COMMAND gets sent only after you press and depress a button) and compare them with the button position/recantgle and then take appropriate action... but that seems too complicated (calculating, storing & comparing coordinates), is there a simpler way ?
thnx
|
|
|
|
|
There are lots of button examples here.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
|
Anonymous wrote: i'm looking for a perhaps simpler/more efficient method (and with using just win32) ...
While only partially related, have you seen this?
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: While only partially related, have you seen this?
hmm thanks for the link but i can't find the handling of WM_LMBUTTONDOWN (and checking if it's for the desired button) in the shown code of that example... am i blind or is it somehow hidden/already implemented by mfc ?
i found a different approach, explained here http://www.pluralsight.com/articlecontent/cpprep0797.htm[^]. it seems simpler and more logical but to implement it for this purpose the button control, that is it's window, should have it's own window procedure which i could then override to handle WM_LMBUTTONDOWN/UP messages... so my question is do even the buttons have a window procedure ?
although when i think about it this approach also isn't as straitforward as it looks, at least not for what i need to use it. in the new wnd procedure i would then have to call GetParent() and then send a message to the parent dialogbox which would then again have to be processed by the dialogbox procedure ...
in your opinion, which method is more efficient then ?
|
|
|
|
|
|
Hi
I am a student.When I do my homework,i encounter a problem.I can not get
the actual value in VisualC++.
The question is how to get 95.123 raised by 12.
Maybe I haven't explained my mean.Sorry about my poor English!
Thanx first!
-- modified at 1:49 Thursday 27th October, 2005
|
|
|
|
|
What are you speaking about ?? What means " 95.123 's 12th power " ?? And what are you trying to do exactly and what is the exact problem encoutered ?
Try to be more specific.
|
|
|
|
|
The question is how to get 95.123 raised by 12.
Maybe I haven't explained my means.Sorry about my poor English!
|
|
|
|
|
You can use the double pow(double,double); for that purpose. Don't forget to include math.h at the top of your file .
|
|
|
|
|
ShiningStarPxx wrote: The question is how to get 95.123 's 12th power.
Do you mean 95.12312 instead? Even using an unsigned long double , you'd get something like 5.4881562051773182e+023. You might could get a bit more precision by using the DECIMAL data type.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
Thank you first!
But i can not catch your means.Would you like to explain it with a sample code.
|
|
|
|
|
#include <math.h>
#include <stdio.h>
void main( void )
{
double x = 2.0, y = 3.0, z;
z = pow( x, y );
printf( "%.1f to the power of %.1f is %.1f\n", x, y, z );
}
Output
2.0 to the power of 3.0 is 8.0
|
|
|
|
|
Oops sorry, the <'s and >'s got removed, the #include's should be
#include <math.h>
#include <stdio.h>
|
|
|
|
|
On paper, 95.12312 is 548815620517731830194541.89902534. However, that number is too big to fit in any type, including an unsigned long double . It would only give you something like 5.4881562051773182e+023 (which equates to 548815620517731820000000.0). Notice the loss of precision. A DECIMAL type may get you a bit more precision.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
Hi,
I have an application working perfectly when compiled in the debug version with optimization off. But the release with optimization on/off and the debug version with optimization on crashes every now and then. Can someone tell me what can be the problem?
I have a places where i do the following :
Literal_Object *lit = (Literal_Object *)pt;
pt is an object Node_Object which is the base class of Literal_Object.
Can this be the cause of my problem?
Thanx in advance.
Mithin
|
|
|
|
|
Mithin wrote: Literal_Object *lit = (Literal_Object *)pt;
Can this be the cause of my problem?
No, not by itself anyway.
See if this helps any.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
i have read this and tried to do everything mentoined in it. There is also a very unusual problem. When i run the release version by clicking on the start button i.e., from the IDE itself my application runs perfectly but when i execute it by double clicking on the exe from the release folder all the problems again appear.
Is there a way i can debug my release version which is executed by double clicking on the file and not running it from the IDE?
Any idea whats happening?
Thanx a lot
Mithin
|
|
|
|
|
Mithin wrote: Any idea whats happening?
Yes: the difference between the two things you mentioned is that for the first one (start it from the IDE) the default working folder will be the folder where your project files resides (.dsw, ...) and for the second one, it will be in the release folder itself (so: ...\YourProject\Release). And so, what is probably happening is that you need some external resources that are not present in the release folder but only in the project folder.
|
|
|
|
|
My project is not using any external resources because the debug version works fine. Do you have any idea which file can it be? Is there a way to avoid application depending on the external sources?
Thanx a lot
Mithin
|
|
|
|
|
Mithin wrote: My project is not using any external resources because the debug version works fine
That doesn't mean anything .
Sorry, I used the wrong word. By 'resources' I meant every file that is used by your program. It could be simply that you try to open and read a file (a simple text file for example) but your code is not secured in case of the file doesn't exist. Do you have anywhere in your program something that uses files located in the same directory ?
|
|
|
|