|
rand()[^]
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Thanks for reply
i use this way
int iSecret, iGuess;
srand ( time(NULL) );
for(int x=0;x<;5;x++)
{
iSecret = rand() % 10 + 1;
}
Then i am getting duplicate number.Sorry sir i want to generate random unique number.
Please help me
|
|
|
|
|
What do you mean you get "duplicate" values? The numbers generated are random. Take a look at another example[^]
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Duplicate values means generated random number repeated.
Now i see example given by you.But result is different.
int i, j, goAhead, timeSeed ;
timeSeed = (int) time(0) ;
printf("Setting seed to be: %d\n", timeSeed) ;
srand(timeSeed) ;
i = 0 ;
for (; i < 10 ; i++)
{
for (j = 0 ; j < 5 ; j++)
{
printf("%d ", rand() ) ;
break;
}
printf("\n") ;
}
Result show more than 10 times.
|
|
|
|
|
you can't generate random numbers between 0 and 1000000 because rand() returns integer less than RAND_MAX which is defined as 32767. So the values returned will be in between 0 and 32767. So the better way is to perform some bit shift operations on the returned random number to make it in the range of 0 to 1000000.
To avoid duplication, suppose if you want to generate 10 unique random numbers from 1 to 10..
int num[10];
for(int i=0; i<10; ++i)
num[i] = i+1;
srand ( time(NULL) );
for(int i=0; i<10; ++i)
{
int index = rand() % 10;
int temp = num[i];
num[i] = num[index];
num[index] = temp;
}
modified on Tuesday, March 9, 2010 7:39 AM
|
|
|
|
|
Thanks Cool_Dev
Problem solved.
|
|
|
|
|
You may read my tip about [^], I wrote about cards in a deck, but the logic is more general.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Unique numbers in 'random' order:
void main( void )
{
const int num = 1000000;
int array[num] = {0};
for (int i = 0; i < num; i++)
array[i] = i;
random_shuffle(array, array + num);
} "One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi all,
I am getting the following error when i try to update the system registry using the following function in InitInstance():
UpdateRegistry(OAT_DOC_OBJECT_SERVER);
"Failed to update system registry.Please try using regedit"
How to resolve this issue. Could not give admin rights to the user.
Thanks,
|
|
|
|
|
|
I was reading about structers in Oriely's c++ and well unfortunately I couldnt buy the book, but I took photos with my phone and what I did what write my own code and i get no errors when I compile it. This is my code:
#include "stdafx.h"
#include <conio.h>
using namespace std;
struct Virtual_Bin{
int Type;
int Quantity;
double Price;
} Produce_Choice;
int _tmain(int argc, _TCHAR* argv[])
{
double i;
Virtual_Bin Produce_Choice;
cout << " Fruit Type: "; cin >> Produce_Choice.Type;
cout << "Fruit Amount: "; cin >> Produce_Choice.Quantity;
cout << " Fruit Price: "; cin >> Produce_Choice.Price;
cout << " Type: " << Produce_Choice.Type << endl;
cout << " Quantity: " << Produce_Choice.Quantity << endl;
cout << "Price Each: " << Produce_Choice.Price << endl;
i = Produce_Choice.Quantity * Produce_Choice.Price;
cout << "Total for quantity of " << Produce_Choice.Quantity << ": " << i;
_getch();
return 0;
}
My Build:
------ Build started: Project: Virtual Structure, Configuration: Debug Win32 ------
Virtual Structure - up-to-date.
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
My output on the screen is:
Fruit Type: Apple
Fruit Amount: Fruit Price: Type: -858993460
Quantity: -858993460
Price Each: -9.25596e+061
Total for quantity of -858993460: 7.95081e+070
|
|
|
|
|
try change to this:
typedef struct VIRTUAL_BIN {
int Type;
int Quantity;
double Price;
} Virtual_Bin;
|
|
|
|
|
Nevermind I figured it out, my:
struct VIRTUAL_BIN{
char Type[32]; <--- was of 'type' int, when it was supposed to be 'char'
int Quantity;
float Price;
} Produce_Choice; modified on Monday, March 8, 2010 9:50 PM
|
|
|
|
|
Dear developers
I'm finishing a simple but useful tool and have a serious problem - when I start my app and access windows registry to create value in RUN key or check if already created (for registering my app to startup) in Vista or 7 it always show a "Do you want to allow...to make changes to your computer?" message which must be confirmed. How can I get rid of this confirmation boxes?
When I confirm the needs, the app registeres itself into startup but after restart it don't run. The key and value is already in registry. This problem occurs on Win 7 and Vista, XP is fine. I'm writing to hkey_local_machine/software/microsoft/.../run
Need I upgrade my compiler to build versions for Vista or Win 7 which can run as Administrator?
I'm using my old but legal VC++ ver7 / 2003 and have C++ 2008 Express too.
Thank you
|
|
|
|
|
tibiz wrote: Need I upgrade my compiler to build versions for Vista or Win 7 which can run as Administrator?
you need to run the EXE as administrator (or at least with elevated permissions). or, turn off the UAC. but don't expect your users to do either.
|
|
|
|
|
no, I can't require from users to turn off the UAC. my app must run at startup like anything other (antivirus, mouse or bluetooth resident, ...). But it won't ...
|
|
|
|
|
Well, the UAC stuff is there SPECIFICALLY to make sure a user gives permission for such activities.
Let's assume that your application DoWonderfulThings.exe does something, erm, wonderful. Let's also assume that you have a backdoor to bypass UAC, so wonderful things can happen a lot. eg, an API called I_Am_Wonderful () .
Let's also assume that I write an application called DoEvilStuff. What's to stop me telling lies (I am evil, after all) and also using the I_Am_Wonderful () API?
This sort of control rightly belongs to the user - not to application developers who can't be trusted.
If all you are doing is checking if a registry value is already there, open the key with read access, not read and write. If you want to write there, then open the key again with write access, and cope with the warnings. This sort of thing should be done at setup time anyway. Or you could install a service that sits quietly consuming negligible resources, just to act as a background actor for you. Or... But all this stuff should require permission from the user... Did you pay for their machine? Didn't think so.
I'm probably a heretic, but I *like* UAC. It forces software writers to shape up, or look bad. My only regret is that it didn't exist earlier. I started 32 bit development and immediately started using HKLM/HKCU properly, and have very rarely hit any security issues.
Iain.
ps, rant over...I have now moved to Sweden for love (awwww).
|
|
|
|
|
ok, I disabled any registry checking but my app still won't run at windows startup on Vista and W7 On XP works properly. Any ideas?
|
|
|
|
|
Does you application consist of Manifest?
|
|
|
|
|
|
If application is running under vista or winows 7 it require manifiest to be added.
Check out the link [^]
A sample Manifest file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="Microsoft.Winweb.appname"
type="win32"
/>
<description>Your app description here</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
level="requireAdministrator" makes the application to be launched with administrative previlages.
|
|
|
|
|
the content of manifest file is linked to executabe by linker or must be distributed into the same folder where the app's executable is running from?
|
|
|
|
|
tibiz wrote: the content of manifest file is linked to executabe by linker or must be distributed into the same folder where the app's executable is running from?
Since, you are using VS2003, the manifest file can be linked to the executable.modified on Wednesday, March 10, 2010 12:19 AM
|
|
|
|
|
I have Dev-c++ version 4.9.9.2 on my computer. When I try to compile anything, it gives me the same error.
<br />
Makefile.win [Build Error] [main.o] Error -1073741515 <br />
I have two different copies of the program on my computer; one that is installed and a portable one and they both give me the same error. I know that it isn't my code because i use the portable one to compile code at school and it works fine there. In advance, thank you for your help.
Here is the compile log I got from trying to compile on the portable version:
<br />
Compiler: Default compiler<br />
Building Makefile: "J:\opengl\Makefile.win"<br />
Executing make...<br />
make.exe -f "J:\opengl\Makefile.win" all<br />
g++.exe -c main.cpp -o main.o -I"J:/Portables/Dev-CppPortable/App/devcpp/lib/gcc/mingw32/3.4.2/include" -I"J:/Portables/Dev-CppPortable/App/devcpp/include/c++/3.4.2/backward" -I"J:/Portables/Dev-CppPortable/App/devcpp/include/c++/3.4.2/mingw32" -I"J:/Portables/Dev-CppPortable/App/devcpp/include/c++/3.4.2" -I"J:/Portables/Dev-CppPortable/App/devcpp/include" <br />
<br />
make.exe: *** [main.o] Error -1073741515<br />
<br />
Execution terminated <br />
|
|
|
|
|
A wild guess: you have a problem with your PATH, the build tools aren't able to locate a DLL file they need, such as e.g. mspdb80.dll (and are not smart enough to provide a descent error message).
|
|
|
|