How to generate random number without repetition in C++

Click here to get straight to the point.

If you are programming in C++ for a while, then you probably got in a situation where you want to get some random number between two given ranges; you might have used old C-function rand() with an initial seed of NULL time, such as the following:

Here, rand() plays a role of random number generator (RNG). A random number generator is simply an object that produces a sequence of pseudo-random values. Note that we must reset the “seed” with an initial value of time when the program is started, else it won’t give random values.

This is quite a common way to get a random number in C. However, the problem that you may have faced with it is that you get the same number several times in your program; that is, it doesn’t distribute numbers properly. That isn’t something you want to do.

In C++, <random> header provides many functions and templates which provide better distribution. With the advent of C++11, it got even more useful functions and random number generators. Following is its usage:

You will now get better distribution. However, it can still give you same number, so this approach can be suitable for programs where it doesn’t matter such as lotteries, etc.

How to generate random number without repetition in C++

Suppose, you are creating a vocabulary builder program, and you want to ask random questions from a player. Now, the program isn’t supposed to ask the same question again from the player when he/she answers a question, but rather it should ask different question every time a player answers one question.

In my previous article, I have told you different ways of finding an element in a container. We will use the approach where two containers (we will use std::vector) will be used: one for storing numbers and other for storing the previous numbers given by our random generator. Following is its implementation:

Here, we are asking for different number from our RNG until the number isn’t present in our container of previous numbers. Now, it will give different number that isn’t given before by random generator in our given range.

Note that std::mt19937 is a RNG called Mersenne Twister 19937. There are some other RNGs as well such as std::minstd_rand, std::mt19937_64, etc. You can use this approach whenever you need to get a random number that is different from the previous numbers.


In this tutorial, I have showed you how to generate random number without repetition in C++. You shouldn’t have any problems now.

Did this article help you in making your programming life easier?

How to find an item in a container in C++

When I was creating a vocabulary builder in C++, I had to find an item in a container of std::vector which stored some values. I looked for its solution on the internet, but couldn’t find what I wanted.

Finding an element in a container or array is quite a common task in programming, yet some programmers don’t know the number of ways it can be done. <algorithm> header provides several useful functions for manipulating containers and one of those functions is std::find, which is especially defined for STL containers like std::vector, std::list, etc.. This can be used on legacy C arrays as well. std::find is used for getting the iterator of a given range.

Through std::find, you can easily find whether an element in a container exists or not. It takes three parameters: First iterator, Last iterator and the element that you want to find in a container. Following is its usage:

If your vector is ordered (if not, then do so with std::sort), then you can use std::binary_search which yields O(log n) worst-case performance, which is way more efficient than the first approach. For sorting elements in a std::vector, you should use std::sort and NOT std::qsort, because the latter is quite expensive for std::vector. Note that if you want to find an element in a container at several times in your program, then you are better off using std::list, because it doesn’t need to be sorted and thus searching is more efficient. Following is its usage:

As you can see, how easy it is to find an element through std::find function.

Another easy way to find an element in a container is by using std::find_if alongside with the lambda feature of C++11. It finds an element in a container with a given condition; if you want to get the first odd / even number in your container that holds numbers, you can define the function for it and then pass it in its parameter. It takes three parameters: First iterator, Last iterator and the function that defines the condition. Following is an example of its usage:

Now you can decide, which way is better suited for your case.

Using std::find on legacy C arrays

If you are using C++, then it is highly recommended to use static / dynamic arrays from STL containers (i.e., std::array, std::vector, etc.) for storing number of elements, but for any reason, if you are using C arrays (i.e., int array[10]), then you can use std::find as well. Following is the way to do that:


In this tutorial, we have learned the different ways to find an item in a container, whether it’s the STL containers or legacy C arrays.

Is there any other way that you know, through which an item in a container can be found in C++? Please let me know in the comments section below!

Operator Overloading In C# With Examples

What is Operator Overloading?

Operator-Overloading in C# that acts as a syntactic sugar in the code and ease the use of libraries in your programs. Most of the built-in operators in C# can be redefined or overloaded; that is, you can give them the meaning of yours!

This feature is not present in C#’s competitor language Java, which makes C# appealing to the many programmers who are building large libraries / frameworks.

Through it, you can create your own user-defined operator implementation of available operators for your own user-defined classes and structs.

This feature is particularly useful if you are creating some library as it will make your library easier to use for others.

In this tutorial, I will teach you its usefulness, misuses and how you can implement them in C#.

Which operators can be overloaded in C#?

In C#, unary operators (“+” as in “i++” etc.), binary operators (“+” as in “obj1 + obj2“ etc.) and comparision operators (“>”, “<” etc.) can be overloaded, while conditional logical operators (&& and ||), array indexing operator, cast operator ([]) and assignment operators (+=, -=, etc.) can not be overloaded. See the full list of overloadable operators on MSDN.

What is the usefulness of Operator Overloading in C#?

Operator overloading is useful in such cases where you want to use basic operations on objects as if those objects are primitives like numbers, strings, etc.

Suppose you have a class for storing three floats (x,y and z) corresponding to mathematical vectors. Let’s call our class “Vector3f”. This class is used for the storing and operating three-dimensional vectors. Mathematical vectors are commonly used in graphics libraries (especially 3D libraries / engines). We need different methods to operate on them: method to add our vector values, subtract them, multiply and divide them, etc. You can create such a class with these methods as follows:

Now, we can use this class in our program and operate on the objects as follows:

Wouldn’t it be cool to simply perform operations on them like “vec1 + vec2“ just like you do “2 + 3“ because it’s pretty obvious from the look that adding them means adding their components (x, y and z)?

Operator overloading on the rescue

Operator-overloading reduces the need to explicitly call methods for such trivial tasks. If you were using Java, you would just stick with the use of methods for operating on vectors due to lack of support of operator-overloading in the core language. In C#, we can use operator-overloading to implement syntactic sugar for trivial tasks like “vec1 + vec2“ through it as follows:

Now, we can simply use them in our program as follows:

Any operator (that can be overloaded) can be overloaded in a class in the following way:

So, if you want to implement equality “==” and inequality “!=”, you can do so as follows:

Similarly, if you want to overload increment “++” and decrement “–” operators, then you can do so as follows:

Now, these overloaded-operators can be used on the objects in the program as follows:

Its common misuses

As it eases the use of libraries, some people just for the sake of small code base in the use of the library, use operator-overloading in places where there is a chance of an ambiguity in its meanings. You should only overload operators on logical types where its meaning is obvious. Consider a class for holding an array:

In this case, we overloaded “+” operator so that we can add any integer to all the members of an array. But, the problem with its usage is that a programmer doesn’t know which number in an array is being added and since there is no addition through indexing, it is pretty ambiguous whether the variable being added is simply an integer or an array of integers.

A simple way to make your code less ambiguous is to provide the equivalent methods for overloaded-operators. So, for instance, if a user of your library can’t understand operators that you have overloaded, then he/she can anyway use methods for achieving the same results.

Note that you should overload in a symmetric manner; that is, if you are overloading “+” and “-”, then it also makes sense to overload “*” and “/”, similarly if you are overloading equality “==” then you should overload inequality “!=” too.

Summing Up

In this tutorial, we have learned the following:

  • Meanings of Operator overloading.
  • Its usefulness when your job is to ease the library usage.
  • Operators which can not be overloaded.
  • Cases where operator-overloading isn’t appropriate.


Operator-overloading is a really useful feature if it is used properly where its meaning make sense. Its usage is perfect in user-defined logical numbers, vector-like structures (quaternions, matrices, etc.) etc.

In short, it should be used in places where the operations on objects are obvious from the look.

Will you be willing to use this feature in your library? Or would you just prefer to use methods for the sake of consistency with your Java code? Tell us your thoughts in the comments section below!