Found 7197 Articles for C++

C++ Program to Emulate N Dice Roller

Ravi Ranjan
Updated on 15-Apr-2025 15:27:13

1K+ Views

Emulating an n dice roller refers to rolling n number of dice simultaneously. In each roll, all the dice will return a different value from 1 to 6. In this article, we are having 'n' number of dices, our task is to emulate rolling n dices simultaneously. The approaches are mentioned below: Using Loop with rand() Function Using Recursion Using Loop with rand() Function In this approach, we have used the rand() function to generate a random value of dice in each roll. The srand() function with time() is ... Read More

C++ Program to Generate Random Numbers Using Multiply with Carry Method

Ravi Ranjan
Updated on 05-May-2025 12:28:21

373 Views

The multiply-with-carry method is a variant of the add-with-carry generator introduced by Marsaglia and Zaman (1991). The main advantages of this method are that it invokes simple computer integer arithmetic and leads to a very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000. In this article, our task is to generate random numbers using the multiply-with-carry method. Here is the formula of multiply-with-carry method: Multiply With Carry (MWC) Formula The multiply-with-carry formula is as follows: Xn = (a * Xn-1 + Cn-1) mod 232 Cn = (a * Xn-1 + Cn-1) ... Read More

C++ Program to Implement Naor-Reingold Pseudo Random Function

Ravi Ranjan
Updated on 02-Jun-2025 14:48:26

216 Views

The Naor-Reingold pseudo-random function uses a mathematical formula for generating random numbers using an array of secret keys('a') and bits of an input number('x'). The generated random numbers can be repeated based on the array of secret keys. In this article, our task is to generate random numbers using the Naor-Reingold pseudo-random function. Formula of Naor-Reingold function The formula for generating random numbers using Naor-Reingold Pseudo-Random Function is given below: Example Here is an example of generating 5 random numbers using Naor-Reingold Function: Input: p = 31, g = 3, n = 4, a = [1, ... Read More

C++ Program to Implement Park-Miller Random Number Generation Algorithm

Ravi Ranjan
Updated on 23-Apr-2025 17:02:49

563 Views

The Park-Miller algorithm is a type of linear congruential generator (LCG) that is used to generate pseudo-random numbers. It is also called the minimal standard random number generator. The general formula of a random number generator (RNG) of this type is: X_{k+1} = g X(k) mod n where the modulus n is a prime number or a power of a prime number, the multiplier g is an element of high multiplicative order modulo n, and the seed X0 is co-prime to n. In this article, we will be using Park-Miller algorithm to generate 5 pseudo-random numbers using C++. Here is ... Read More

C++ Program to Generate Random Numbers Using Probability Distribution Function

Ravi Ranjan
Updated on 22-Apr-2025 11:38:55

2K+ Views

The probability density function (pdf) is a function that describes the relative likelihood for this random variable to take on a given value. It is also called as density of a continuous random variable. The probability of the random variable fall within a particular range of values is given by the integral of this variable's density over that range, So, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range. Probability Distribution is based upon this probability density function. In this article, we will generate ... Read More

C++ Program to Generate Random Numbers Using Middle Square Method

Ravi Ranjan
Updated on 22-Apr-2025 11:38:27

1K+ Views

The middle-square method is one of the simplest methods of generating random numbers. This method will either begin repeatedly generating the same number or cycle to a previous number in the sequence and loop indefinitely. For a generator of n-digit random numbers, the period can be no longer than the specified n(number of digits). If the middle n digits are all zeroes, the generator then generates zeroes forever. In this article, we will implement a C++ program to generate ten 4-digits random number using the middle-square method. Steps for Middle-Square Random Number Generation The steps for generating random numbers ... Read More

C Program to sum the digits of a given number in single statement

Ravi Ranjan
Updated on 15-Apr-2025 15:27:34

571 Views

The calculation of the sum of the digits of a number in a single statement means that only one line of code will be doing the addition operation. The addition operation should not extend more than one statement. In this article, we have a number and our task is to calculate the sum of the digits of the number in a single statement in c. Here is a list of approaches: Using for Loop Using Recursive Function Using for Loop This approach uses a for loop to calculate the ... Read More

C++ Program to Implement the linear congruential generator for Pseudo Random Number Generation

Ravi Ranjan
Updated on 08-May-2025 18:49:19

1K+ Views

The Linear Congruential Generator (LCG) is a very simple technique to generate a sequence of numbers that looks like random numbers but is actually determined. It is one of the reasons to call it a pseudo-random number. The Linear Congruential Generator (LCG) technique generates a random number based on the previous number and uses a linear recurrence to generate the sequence of random numbers. In this article, we have set an initial value of Xn and defined the value of the constants. Our task is to generate pseudo-random numbers using the linear congruential generator in C++. Formula of Linear ... Read More

C Program to print numbers from 1 to N without using semicolon

Ravi Ranjan
Updated on 15-Apr-2025 15:27:59

2K+ Views

Printing numbers from 1 to N is an easy task that can be achieved using loops, but to print numbers from one to N without using a semicolon is a tricky question. We will discuss two different methods to solve this question using iteration and recursion approaches. In this article, our task is to print numbers from 1 to 'n' and we don't have to use a semicolon. The approaches are listed below: Using Iteration Using Recursion Using Iteration This approach uses the iterative approach, where we have used the while loop to print the numbers from 1 to N. The num

C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle

Ravi Ranjan
Updated on 28-May-2025 12:12:00

640 Views

The Euler path is a path by which we visit every edge exactly once. We can use the same vertices for multiple times. The Euler Circuit is a special type of Euler path. When the starting vertex of the Euler path is also connected with the ending vertex of that path, then it is called the Euler Cycle. In this article, our task is to check if the Eulerian cycle exists in the given directed graph or not. In the above figure, there is a directed graph and its respective adjacency matrix. The Eulerian path respective to the ... Read More

Advertisements