answersLogoWhite

0

How do you generate two random numbers in C plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int GetRand( int min, int max)

{

// Swap min and max if wrong way around.

if(max<min)min^=max^=min^=max;

return(rand()%(max-min+1)+min);

}

int main()

{

// Seed the random number generator with current time to ensure

// random numbers are different on each run.

// Note: use srand(1) to generate the same sequence on every run.

srand((unsigned)time(NULL));

printf("Number from 0 to 10 : %d\n", GetRand(0,10));

printf("Number from 1 to 100: %d\n", GetRand(1,100));

return(0);

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you generate two random numbers in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you make cmd generate a random number between two variables that can be changed at anytime?

In DOS, the following line will generate a random number between 1 and 100. To get 0-100, remove the plus one at the end. set /a num=%random% %%100 +1


How are cell phone numbers assigned?

When telephone numbers were invented, they were short - as few as two or three digits - and were used by people to call a few neighbors. As phone systems have grown and interconnected to encompass the world, telephone numbers have grown longer and more complex, and in addition to telephones they now designate many other devices, such as computers and fax machines.


Why do you use pseudorandom numbers in simulation?

Generation of random numbers is not a simple process. In order for a number to be truly random it must be uniformly distributed (each random number in the interval of random numbers has equal chance of being selected), efficiently generated (the random numbers hsould not degenerate into constant values or recycle too frequently) and absent of patterns. Computers can generate random numbers using a mathematical process that artificially creates psuedorandom numbers more efficiently than true random numbers can be generated in a process akin to spinning the roulette wheel.


What are two prime numbers that would generate 400?

There are no two prime numbers that would generate 400. The prime factorization of 400 is 2^4 * 5^2, which means it can be expressed as the product of two powers of primes, but not as the product of two distinct prime numbers.


What is the probability of getting two prime numbers 1-10?

The probability of getting two prime numbers when two numbers are selected at random and without replacement, from 1 to 10 is 2/15.


Write a java application that generates two random numbers using built in method math random ranging 5 to 8 the program should use another built in method to calculate the power of xy the x value is t?

write a java application that generate custemer account balance in a banking system?


What is plus times plus?

The product of two positive numbers is positive.


Is 27 6 a sum?

No, it is not. Those are just a random group of numbers. A sum is an amount resulting from the adding of at least two numbers.


What numbers is it that twelve plus what equals two?

(-10)


How do you generate 2 4 6 10 16 26?

By adding the differences of the previous two numbers.


What are the next two numbers in 2 8 32 128?

There are infinitely many polynomial functions that generate those 4 terms, so what two numbers would you like to be the next two and I'll generate a polynomial function to create those 4 plus the two you want. However, the simplest relation between the terms is a Geometric Progression with a constant difference of 2&sup2; = 4, making the next two numbers 128 &times; 4 = 512, 512 &times; 4 = 2048. The nth term of this sequence is given by: u(n) = 2^(2n-1) {for n &ge; 1}.


Does a plus b equals b plus a for any two whole numbers?

Yes.