Duplicate output when using srand for random seed [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm using srand (time(NULL)); to generate a random seed.
Problem is, I'm submitting 30+ identical jobs to a LINUX cluster. If I submit them one at a time, everything is fine, but of course I prefer to use a batch job to submit all 30 at once. Much easier and quicker. Problem is, then several batches of the jobs all appear to access exactly the same time, and I get duplicate results! Can anyone suggest an easy solution to this?

Consider reading from /dev/random or /dev/urandom. They have higher quality randomness than rand() (which is usually just a simple linear congruential generator), and /dev/random blocks until sufficient entropy has built up.

Look into the new features in <random> in C++11. In particular std::random_device. Otherwise, a cheesy solution is to add pid to time(NULL).

Related

How do I compare two functions' speed and performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two functions performing same process but with different techniques and I need to know on a large scale which technique is faster than the other of maybe in the future will be more techniques available. So my question is, how can I do that in c++ specially? Is there a specific method and header to be used to perform this task?
More details:
For example the isLargest() uses three parameters and it has two versions, one uses a nested if technique and the other uses initializers and less if statements. So if I need to know which one is faster, how can I do that?
Try your code in the real world and measure
There is a tool called a profiler that is meant to solve this problem. Broadly speaking, there are two kinds (note: some are a mix between the two):
Sampling profilers.
Instrumenting profilers.
It's worth learning about what each does and their pros/cons, but if you don't know what to use go with a sampling profiler.
There are many sampling profilers, but support depends on your platform. If you're on Windows, Visual Studio comes with a really nice sampling profiler and I recommend you start there!
If you go down this route, it's important to make sure you use your functions as you would "for real" when you're profiling them, as there are many subtle factors that can affect the result.
An alternative
If you don't want to try your code running in a real program, perhaps if you're just trying to understand general characteristics of the function, there are libraries to help you do this such as Google Benchmark.
Benchmarking code can be surprisingly difficult to get right, so I would strongly recommend using existing benchmarking tools where like Google Benchmark wherever possible.

Elegant way to excecute many alarm clocks? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's say I have a vector of exact future times of multiple events. I want those events to launch when their respective times have been reached. How do I do this without constantly using if statements to check that the current time is one of those set times? This constant checking, especially if many other things are running in my program (and some of those times may be a bit away from now) will reduce the performance of my program. I'm using c++11, latest version of GCC.
If you're using C++11, it's probably easiest to create a thread, have it sleep until the next alarm time. The most efficient way to store the times for the alarms is probably a priority queue.
If you don't have C++ 11 available, you might consider Boost Threads instead. The standard's threads are based closely on Boost threads, so using them will be fairly similar.
If you don't want to use that, you'll pretty much need to use operating system facilities to do the job. With almost any reasonably modern OS, you can create a thread and have it sleep until the next alarm time, but you'll probably also have something that supports what you want a little more directly (call a function at a specified time). The details of that will be specific to the OS though.

Ideas for storing and operating on huge numbers in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am supposed to build a program for storing and handling huge integers. I know that there are many answers out there but I need ideas that I can implement easily, bearing in mind that I can use any of the basic concepts of C/C++.
How should I go about it?
This is the first time I am asking a question here so please correct me if I am wrong about anything.
Edit: Actually what I wanted to know was how should I go about storing a huge integer... Obviously an array is what comes to mind at first glance but are there any other methods out there at the basic level?
EDIT2: I came across a very nice solution to this problem a while ago, but was just a bit lazy to put it on here. We can use the concept of number systems to deal with huge numbers. We can declare an array that holds the co-efficient of powers of 256, thus obtaining a base 256 system. We can then use fundamental concepts like those of the various number systems to obtain our required results.
Matt McCutchen has a Big Integer Library
If you want to do this yourself his code would be a great starting point. As you can overload arithmetic operators in C++ it is not too difficult to make a new BigInteger class and make this handle any number of bits per integer.
There is also a stack overflow answer to this question: here
I consider this as a question about theory, as such I suggest to browse the internet using the right keywords for documents/articles or to take a sneak peek at libraries that are implementing this feature and are well tested, this projects also tend to offer a mailing list or a forum where developers can communicate, it can be a good place to start writing about this stuff.

On what amount of work does using OpenMP start making sense? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I recently started looking on parallelization using OpenMP and found a decent amount of good resources describing how to use it. However, I was unable to find documentation on when parallelization starts making sense or in other words: where is the turning point where parallelization start compensating the overhead of OpenMP's thread creation and in what cases is it better to go without it? How complex has work to be so it makes sense to parallelize it?
Is there any documentation or guide available on that?
From my own experience, if your computation is well suited for parallelization, you can expect substantial gain if the serial computation (for example the loop you want to parallelize) takes a few milliseconds.
Below 1 millisecond, it will not help to use multiple threads due to the overhead involved.
Image processing would be one good example...
I used it when running sift and surf on two consecutive images..
I find it useful if you need to do heavy mathematical calculations especially on matrixes..

Real-time pitch detection using FFT [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to do real-time pitch detection using C++. I'm testing some code from performous (http://performous.org/), because everything else hasn't worked for me. I know for sure that this works, but i just cant get it to work. I've been trying this for a few weeks now, and I haven't been able to get any pitch detection code working.
Instead of using input from the mic, you should create data of a known single frequency and run that through the program and see if it gets you the correct result. Then you can add harmonics to it and see if that works. Real world data is just too variable for initial testing.
Performous audio code has some optimizations, frequency limits and heuristics that make it only suitable for singing (and other similar tones). The optimal range is around 80-600 Hz.
C/C++/Obj-C Real-time algorithm to ascertain Note (not Pitch) from Vocal Input
Check the accepted answer on this link.
I have scoured SO for an answer to this problem, and this is the most useful resource I have found.
It appears that Performous uses this algorithm, but it's hard to make out from the Performous code
EDIT: I have finally managed a working solution. e-mail me if interested sunfish|gmail|c0m