Question:
Output random variables in C++?
ianoniymous
2010-12-04 13:43:28 UTC
Let me say this as short as I possibly can:
I'm learning C++, and I only know VERY basic things in it so far.

My question is:
What would I type to have Command Prompt output random letters/variables?

There are 2 programs I wish to write using this.

One merely outputs random letters from a pool of consonants B-Z excluding vowels (including y), and then every 2 or 3 consonants, spits out a vowel consisting of A, E, I, O, U, and Y.

The other one requires the user to put in 2 or more things, and have it spit back one of them at random.

Thanks!
Six answers:
Wajahat Karim
2010-12-04 14:21:03 UTC
You will need to type rand() function to generate random. This function will generate a random number between 0 and 1. but you will observe these values will be repeated after some time, so we need to synchronize these values on time. so for that purpose we will use srand() function, which will take a time to synchronize.



so here are your required two programs ,,,,,





************* program 1 *************



#include

#include

using namespace std;



const int CONSONANTS = 20;

const int VOWELS = 6;

const int ARRAY_SIZE = 10;



int main ()

{

char consArray[CONSONANTS] = "BCDFGHJKLMNPQRSTVWXZ";

char vowelArray[VOWELS] = "AEIOUY";

char array[ARRAY_SIZE];



time_t t1; //for time synchronization

srand(&t1); //synchronizing time with t1

int randomNum = 0;



//Generating random letters

for (int i=0;i
{

if (i%3 == 0) //If it is third letter,generate vowel

{

randomNum = rand() % VOWELS;

array[i] = randomNum;

}

else //otherwise generate consonant

{

randomNum = rand() % CONSONANTS; //we need number from 0 to 20

array[i] = randomNum;

}



//Printing Output

for (int i=0; i
{

cout<
}

return 0;

}





**************** program 2 ********************



i will take 5 numbers from user and choose 1 randomly from them and print it.....



#include

#include

using namespace std;





const int ARRAY_SIZE = 5;



int main ()

{

int array[ARRAY_SIZE];

int randomNum = 0;



time_t t1; //for time synchronization

srand(&t1); //synchronizing time with t1



cout<<"Enter any 5 numbers: ";

for (int i=0; i
{

cin>>array[i];

}



randomNum = rand()%ARRAY_SIZE;

randomNum = array[ randomNum ];



cout<


return 0;

}





i hope you have got what you were looking for,,,,,
?
2010-12-04 13:56:06 UTC
create two arrays one containing consonants and another containing vowels. then using rand() % sizeOfArray get a random number from 0 - size of your array and use that number as the index of the array. if you want every 2-3 to be random do rand() % 3 + 2 and have that as a flag. So you print the index of the array then subtract from the flag repeat (put it in a while loop or for loop) then once the flag == 0 use rand() % sizeOfVowelArray for the index of your vowel array to print...



This is a pretty easy way of doing what you are looking for otherwise you would have to create a random number check to see if its ascii value is a vowel if not print it if yes get a different random number. you would still need a for or while loop with a flag if you want a random 2-3 consonants then a vowel





part 2:

use int main(int argc, char *argv[]).



when you run the program from terminal you would do ./program param1 param2 param3... and that would all be stored in argv[] and you just randomly get a value so rand() % argc and output the index of argv.



otherwise you can use stdin.h and cin >> string/char * variable and traverse that counting the whitespaces before the end create an array of that size and put each word into the array then get a random index of it print that out.
green meklar
2010-12-04 18:35:42 UTC
Here are two functions I use for producing random integers:



int z(int minz,int maxz)

{

return (((rand( )*rand( ))+rand( ))%(max( 1, maxz-minz )+1))+minz;

}

void s()

{

srand( (unsigned)(time( 0 )+z( 0, 16384 )) );

}



Call z() with a minimum and maximum integer passed to it in order to retrieve a random integer in that range. For instance, let's say I did this:



for(int i=0;i<5;i++)

{

cout<
}



the program, upon reaching this loop, would output a vertical list of five integers from 1 to 6 inclusive. That is to say, each number in the list could be 1, 6, or any integer between 1 and 6. Of course, you don't have to output the integers, you can use them for other things as well. For instance, let's say you want something to happen with an 83% chance, you could do this:



if(z(1,100)<=83)

{

do_stuff_here();

}



You could also use them as array indices, or inputs for switch statements. You could cast them to other data types. And you could add and multiply them together to get biased ranges of numbers. The possibilities are nearly endless.



You may have noticed the s() function. It does not return anything. Its purpose is to reseed the random number generator. Strictly speaking, you don't need it. You could just use z() directly (in which case you should always get the same sequence of random numbers given the same input range), or you could seed the RNG manually. However, s() has been designed to maximize the level of randomness when reseeding multiple times very quickly one after the other. You should call s() once when the program first starts, and it can help to call it again before any large, repeated code blocks involving a lot of calls to z(), for instance a function intended to generate a complex data structure with random features.



Furthermore, z() is also more complex than it strictly needs to be. The additional complexity allows it to take wider ranges of numbers (although the function is still not perfect and it is advised not to use ranges larger than a few tens of thousands) and provide increased randomness, at the expense of some extra computational overhead. The versions of the functions shown here are designed for generators. You may want to use different versions for, say, real-time computer games.



You can also of course rename the functions if you like. I used very short names because I use these functions a lot (especially z()) and I don't want to type a long name every time. Be aware though that if you rename z() you will also have to change the call to z() inside s().
SomeGuy
2010-12-04 14:23:19 UTC
Generating random numbers is easy but generating letters is the trickier part.

Now you may want to look into ASCII characters as you could convert those from numerical values to letters.



Now I'll tell you how to generate a random number in C++ and then from that you can figure out letters.

Also you can say that is the number was 1 then it should spit out a with a switch statement



switch (number) {

case 1:

cout << "A";

case 2:

cout << "B";



}



Now since you're a beginner you might not know what a switch statement is so here's a link to a tutorial on youtube.



http://www.youtube.com/watch?v=kTJK8Z18Pbc



Now to generate a random number in c++ is fairly easy but I really don't feel like typing it out right now so here's a useful link that will show you what you need to know.



http://www.youtube.com/watch?v=sbrx1vt0NF8



If you need further help from him just send him a message in his inbox and he'll help you out.



Hope This Helped :D
?
2016-10-19 13:06:21 UTC
you shouldn't seed the random selection generator on your functionality that computes the cube rolls(!) you will no longer get random numbers that way for each seed you get the comparable series of numbers so in case you seed it the comparable each time you call the functionality you will get the comparable numbers. int rollDice (void) { //Create variables int i; //Generate and output cube rolls for (i=a million; i<=10; i++) { //start up at i==a million and bypass 'til i > 10 // no longer solid prepare. in case you decide directly to loop 10 cases this is greater advantageous to // say (i=0;i<10;i++) .... this is smart to keep in mind on the grounds that array indexes are 0 based so if your array has 10 aspects they are numbered from 0 to 9 -- this is solid to get into the prepare of counting from 0. printf("%10d", a million + rand() %6 ); // print the random selection modulo 6 -- this is the rest after dividing by using six and including a million if (i % 5 == 0) // print a newline if i is divisible by using 5 with out the rest printf("n"); } return 0; // return a 0 } this could be greater advantageous style: #incorporate #incorporate int rolldie(void); int significant(void){ int x; srand(time(0)); // seed the random numbers as quickly as -- use the present time for(x=0;x<10;x++) printf("%d ",rolldie()); return 0; } int rolldie(void){ return a million + rand() %6 ; }
peter
2014-11-04 19:19:55 UTC
problematic issue. search onto a search engine. just that can assist!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...