Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What are Randomized Algorithms?
Need for Randomized Algorithms
Classification of Randomized Algorithms
Monte Carlo Algorithms
Freivalds’ algorithm
Las Vegas Algorithms
Relation between Monte-carlo and Las Vegas
Advantages of Randomized Algorithms 
Limitations of Randomized Algorithms
Frequently Asked Questions
Q. What is a randomized method?
Q. Why are randomized algorithms faster?
Q. What is a randomized select algorithm?
Q. What are the two main types of randomized algo?
Q. What is the random algorithm in OS?
Last Updated: Mar 27, 2024

Randomized Algorithms

Author Shivani Singh
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Have you ever flipped a coin to make a decision? Congratulations, you've just used a randomized algorithm! Unlike the deterministic algorithms you might be familiar with, which follow a set of rules to reach a single answer, randomized algorithms incorporate a bit of luck. They use random numbers to guide their decisions, often leading to faster solutions or even tackling problems that deterministic approaches can't handle. In this blog, we'll discuss randomized algorithms.

Randomized Algorithms

Randomized Algorithms use some randomness in their logic to improve efficiency, time complexity, or maybe the total memory used. Let us dig deeper into the blog; Randomized Algorithms to know more.

What are Randomized Algorithms?

Randomized algorithms are a class of algorithms in computer science that use randomness or randomness in combination with deterministic steps to solve computational problems. These algorithms introduce randomness intentionally into their calculations to achieve specific goals, such as improving efficiency or increasing the likelihood of finding a correct solution.

Randomized algorithms are particularly useful in situations where finding an exact solution to a problem is computationally expensive or impractical. They often provide approximate solutions with a controlled level of error. Some common applications of randomized algorithms include:

A Las Vegas or a Monte Carlo are the two most frequent algorithms to build randomized algorithms. We will see many things about them in the later section of the blog.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Need for Randomized Algorithms

Several questions must arise in your brain, like why do we even need to add randomness to our logic? What is the requirement of doing so? For example, if we have a quick sort that works fine, why do we add randomness to its logic?

This section of the blog will give you a few advantages of Randomized algorithms that will help to clear most of your doubts regarding the need for Randomized algorithms.

Need for Randomized Algorithms


  1. Improved Efficiency: Adding randomness to the logic of an Algorithm might reduce the likelihood of that worst-case scenario happening, thus increasing its efficiency.
  2. Complex Problems can be Handled Effectively:  Randomized algorithms are suitable for difficult situations because they manage complex issues that are difficult to solve deterministically by providing approximate solutions or many other techniques.
  3. Worst-Case Scenarios can be avoided: we have already discussed this advantage of randomized algorithms. Take the example of the quick sort; introducing random pivot selection in the logic can reduce the probability of falling into the worst-case scenario of the algorithm, improving the overall time complexity.
  4. Cryptography: In cryptography, randomness is essential for creating secure keys, ensuring unpredictable results, and protecting against attacks. To increase security, randomized algorithms are used in random number generation, encryption techniques, and cryptographic protocols.

Looking at the above advantages of randomized algorithms we can safely say that randomized algorithms not only increase the efficiency of the program but also sometimes increase the security of the program.

Also Read - Selection Sort in C

Classification of Randomized Algorithms

Randomized algorithms can be roughly grouped into numerous categories based on their properties and applications. Here are two broadly classified categories of randomized algorithms.

Classification of Randomized Algorithms


  1. Monte Carlo Algorithms: To approximate numerical values or solve issues probabilistically, Monte Carlo methods use randomization. With a certain degree of assurance, they offer an approximate solution. Example includes, calculating the value of π, resolving optimization problems, or doing probabilistic simulations.
  2. Las Vegas Algorithms: A Las Vegas algorithm executes in a predetermined period of time. It is predictable that it runs out of time and doesn't find any solutions, but if it finds one within that window, it will be precisely correct.

Note: Due to the algorithm's potential for producing inaccurate results, an algorithm called as amplification is utilized to increase the likelihood of correctness while degrading runtime. The randomized algorithm is used numerous times with various random subsamples of the input, and the results are then compared to achieve amplification.


Let’s discuss each one of them one by one in the next few sections of this blog.

Monte Carlo Algorithms

The goal of the Monte Carlo approach for randomized algorithms is to complete the execution within the allotted time limit. This method's running time is therefore predictable. For instance, when performing string matching, Monte Carlo starts the procedure over from the last error it encountered. Thus, time is saved.

deterministic algorithm's output is always anticipated to be right, whereas Monte Carlo techniques can not guarantee this. These algorithms are typically categorized as either false-biased or true-biased for decision issues. When a false-biased Monte Carlo algorithm delivers false, it is always correct; likewise, a true-biased method always yields true.

Freivalds’ algorithm

One interesting example of a randomized algorithm is Freivalds' algorithm. This algorithm tackles the task of verifying matrix multiplication. Imagine you have three matrices: A, B, and C. A standard approach would be to multiply A and B and compare the result to C element by element. However, Freivalds' algorithm offers a more efficient solution that leverages randomness.

Here's the basic idea:

  1. Freivalds' algorithm generates a random vector with only 0s and 1s.
  2. It multiplies this random vector with both B and C.
  3. Finally, it checks if the product of A and the random vector multiplied by B is equal to the product of the random vector multiplied by C.

If the equation holds true, it's very likely (but not guaranteed) that A x B = C. If not, then the multiplication is definitely incorrect.

This approach might seem strange at first, but the beauty lies in its efficiency. By performing just a few multiplications with the random vector, Freivalds' algorithm can verify the matrix multiplication with high probability, significantly faster than the traditional method.


Let's consider a simple example to illustrate Freivalds' algorithm:

  • Matrix A: [[1, 2], [3, 4]]
  • Matrix B: [[5, 6], [7, 8]]
  • Matrix C (correct product of A and B): [[19, 22], [43, 50]]
  1. Random Vector: Generate a random vector, say [1, 0].
  2. Multiplication:
    • A * random vector = [[1, 2], [3, 4]] * [1, 0] = [1, 3]
    • Random vector * B = [1, 0] * [[5, 6], [7, 8]] = [5, 7]
    • Random vector * C = [1, 0] * [[19, 22], [43, 50]] = [19, 43]
  3. Verification: Check if A * (random vector * B) = (random vector * C). In this case, [1, 3] * [5, 7] = [19, 43] which is equal to [19, 43].

Since the equation holds true, Freivalds' algorithm suggests (with high probability) that A x B is indeed equal to C, which is correct in this case.

Las Vegas Algorithms

This is a randomized algorithm that consistently yields the right answer or fails. Still, it cannot guarantee a time limit because the time complexity depends on the input. Virtually every search result contains a Las Vegas algorithm. Consider a Las Vegas algorithm as a problem whose answer, when found, is certain to be the right one but whose route to that answer may be uncertain.


The term "Las Vegas" for this algorithm is credited to mathematician Laszlo Babai, who came up with it in 1979 simply as a comparison to the much older Monte Carlo algorithm as both are significant global gaming centers.


Quicksort is a common Las Vegas randomized sorting method that uses no additional memory and sorts elements in place. Since this technique is comparison-based, the worst case will happen when doing a pairwise comparison, which takes O(n^2) time complexity, where the time needed grows as a square of the number of digits to be sorted. However, with randomization, this algorithm's worst-case time complexity can be lowered to O(n log(n)).

# Randomized Algorithms Pseudo Code
	Array A with total n elements
def Randomized_quicksort(A):
	If n = 1:
		# Array A is already sorted.
		return A 
		i = Random number in range(1, n)
		# the pivot element using random method
		X = A[i]	
		Partition A into elements < x, == x, and >x
	Apply Quicksort on A[1 to i-1] and A[i+1 to n].
	Combine all the data to obtain a sorted array.


If the pivot element selected at random is the first or final member in the array, the worst-case scenario for this technique is that it takes O(n^2) time to sort n digits.
The anticipated runtime for randomized algorithms, i.e., Quicksort, where the pivot is selected randomly, and neither the smallest nor largest number in the array is chosen, is O(n log(n)).


Let's consider sorting the list: {8, 3, 1, 4, 2, 7, 6, 5}

  1. Choose pivot: We'll take the first element as the pivot (8).
  2. Partitioning: Rearrange the list such that elements less than 8 are on the left and elements greater than 8 are on the right. Here's a possible partitioning: {3, 1, 4, 2, 5, 7, 6, 8} (Notice 8 is now in its potential sorted position).
  3. Recursion: Now, we recursively sort the two sub-lists:
    • Left sub-list: {3, 1, 4, 2, 5} (applying Quicksort again)
    • Right sub-list: {7, 6} (already sorted as it only has two elements)

By recursively sorting the sub-lists and combining the results, we get the final sorted list: {1, 2, 3, 4, 5, 6, 7, 8}.

Relation between Monte-carlo and Las Vegas

By executing a Las Vegas algorithm for a predetermined amount of time and producing a random response when it fails to terminate, it can be transformed into a Monte Carlo algorithm.
Below is the table that shows the relation between two algorithms.


Monte Carlo Algorithm

Las Vegas Algorithm

Correctness Probabilistic Certain
Running Time Certain Probabilistic

Now let us discuss some frequently Asked Questions on Randomized Algorithm.

Advantages of Randomized Algorithms 

Advantages of randomized algorithms are:

  • Efficiency: They often provide faster solutions, especially for complex problems.
  • Simplicity: They can be simpler to implement than deterministic alternatives.
  • Approximate Solutions: Useful when exact solutions are hard or unnecessary.
  • Probabilistic Correctness: Offer high probability of correctness, balancing reliability and speed.
  • Versatility: Applicable in various fields like computer science, math, and optimization.
  • Parallelism: Easily parallelizable, making them suitable for modern computing architectures.
  • Privacy: Useful for ensuring data privacy in scenarios like cryptography.
  • Robustness: Resilient to variations and uncertainties, valuable in real-world scenarios.

Limitations of Randomized Algorithms

Limitations of randomized algorithms are:

  • Probabilistic Output: They provide approximate solutions with a probability of correctness, not guaranteed accuracy.
  • Analysis Complexity: Evaluating their performance can be challenging due to randomness.
  • Deterministic Alternatives: In some cases, deterministic algorithms provide certainty at the cost of speed.
  • Difficulty in Reproduction: Results may vary between runs, making them less predictable.
  • Resource Usage: May consume more memory or time due to random operations.
  • Not Always Suitable: Unsuitable for tasks requiring exact, reliable results.
  • Complexity: Designing and analyzing randomized algorithms can be complex.
  • Error Control: Ensuring the acceptable level of error requires careful consideration.

Frequently Asked Questions

Q. What is a randomized method?

A randomized method is an approach that uses randomness or chance as a fundamental part of its process. It introduces randomness intentionally to achieve specific goals or outcomes.

Q. Why are randomized algorithms faster?

Randomized algorithms can be faster because they use randomness strategically to make quick decisions or approximations. This randomness can lead to faster average-case performance, especially in complex or uncertain situations.

Q. What is a randomized select algorithm?

Randomized select algorithm finds the i-th smallest element in an array efficiently using randomness (average time complexity of O(n)).

Q. What are the two main types of randomized algo?

The two main types of randomized algorithms are:

  1. Monte Carlo: Use randomness to solve problems probabilistically, often with good average-case performance (e.g., randomized quicksort).
  2. Las Vegas: Guarantee a correct solution but with a running time that depends on random choices (less common).

Q. What is the random algorithm in OS?

The random algorithm in OS is used in various areas like task scheduling (balancing workload), memory management (page replacement algorithms), and caching (replacement policies). 


As we have come to the end of this blog, let us see what we have discussed so far. In this blog, we have discussed the Randomized Algorithms along with their need. After that, we discussed the classification of randomized algorithms which are Monte Carlo Algorithms and Las Vegas Algorithms. In the end, we discussed the relation between Monte Carlo and Las Vegas.

If you like to learn more, you can check out our articles: 


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Previous article
Zhu-Takaoka String Matching Algorithm
Next article
Priority Scheduling Program in C
Live masterclass