answersLogoWhite

0


Best Answer

Use the random number generator classes.

#include<random>

#include<ctime>

std::default_random_engine generator ((unsigned)time(0)); // seed the generator

std::uniform_int_distribution<int> distribution (0, 100); // set the closed range

int r = distribution (generator); // pick a random number from 0 to 100 (inclusive)

Any device can be used to seed the generator. Here, we've simply used the current time. If you don't seed the generator, it will generate the same sequence of random values every time the program is run.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

You don't calculate rand: you simply seed the random number generator and select a random number from the generator.

#include <iostream>

#include <time> // required for seeding

int main()

{

// Seed random number generator from current time:

srand(( unsigned ) time( NULL ));

// Set the range (1 to 10 inclusive)

int upper = 10;

int lower = 1;

// Select a random number from the range.

int r = ( rand() % (( upper - lower ) + 1 )) + lower;

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include

And then in your code:

rand()%(n+1)

Will return an integer between 0 and n.

rand()/float(RAND_MAX)

Will return a decimal value between 0.0 and <1.0.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

There's no way to guarantee this other than to keep track of which numbers are produced and skipping over those that have already been selected. However, keep in mind that random number generators select real numbers n such that 0.0 <= n < 1.0, and there are an infinite number of values in this range. Although many of those values cannot be represented accurately, there are still far too many to make this method practical.

Using a range of integral values is easier to check because the range of values between any two integrals is finite. Store the integrals in a vector then select an index at random (0 to n-1 for n integrals). Whichever index is chosen, swap the value at that index with the value at index n-1 (the last index). Subtract 1 from n to eliminate the last element from the next selection, repeating until n is 1. In this way every number will be selected exactly once, only the order in which they are selected will change (assuming you change the seed value on each execution). Note that the vector will contain the integrals in the reverse order they were selected. This is akin to shuffling a deck of cards by selecting a card at random and placing it in a new pile, repeating until there is only one card remaining which simply goes on top of the new pile.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Pseudo-random numbers can be generated with function randor random.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you calculate rand in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is randomize function in c plus plus?

You can use rand(). Make sure that you use srand()to generate the seed for rand() (if you do not, you get so called pseudo random numbers).


What is rand function in c plus plus?

The rand() function returns an integer (int) value that represents a pseudo-random number between 0 and RAND_MAX, RAND_MAX being a constant declared in the run-time library. Each time rand() is invoked, a different value is returned.


How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).


How do you calculate percentage of marks obtained in 1 subject in c plus plus?

You can't, you have to come up with variables to calculate grades


What has the author Marguerite C Rand written?

Marguerite C. Rand has written: 'Ramo n Pe rez de Ayala'


Can anyone give you the c plus plus code to check where the line ax plus by plus c cuts the circle x2 plus y2 plus 2gx plus 2fy plus c equals 0 where a b c g f are entered by the user?

Your question is not clear. Please give the formula to calculate the value of x and the condition at which the line cuts the circle


C plus plus program calculate the power value of input base and exponent numbers?

cn = c0 *( 1 + i ) pow n


What is 100 R in a C in C?

100 cents in a Rand


How do you calculate formula for a plus b plus c plus d equals 12 when a equals b plus c and c equals d?

There are four unknown variables and only three linear equations so there is not a unique solution. All you can do is to rearrange the four variables so that three of them can be expressed in terms of the fourth. For example: In terms of c, a = 6 - c/2 b = a - c = 6 - 3c/2 d = c


What has the author Neill C Ostrander written?

Neill C. Ostrander has written: 'The Rand Sync-Sat Calculator' -- subject(s): Rand Sync-Sat Calculator, Syncom (Communications satellite)


Can anyone give a code for c plus plus to do a random dice roll for a dropped item when a crate is destroyed?

int die = ((double)rand())*6/RAND_MAX+1; // result is 1, 2, 3, 4, 5, or 6


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c