Hi ,
I ld to print out characters and intergers together randomly?
for example .. k 10 5 r 3 .. etc..
But I set 4 char and 10 int(frm 1-10) ..
pls help me out
thz in advance!:)

it seem like character and number out by rand().
I can do just only in one type.. 3 5 8 .or k g d ...
But what i want is to mix two type together by rand().
So is there way to do like that?
:rolleyes:

look at an asscii chart and you find that the ascii value of the letters 'a' to 'z' are 97 thorugh 122 respectively.

This will return a character beteween 'a' and 'z'. Use the same technique to get a number between 1 and 10.

rand() % ('z' - 'a' + 1) + 'a';

>>But what i want is to mix two type together by rand().
put the above in a loop and on every other loop iteration generate a character and on every other loop iteration generate the integer. Use the mod operator on the loop counter to determine which one it is

Here is a short tutorial on random numbers that may help you.

But what i want is to mix two type together by rand().
So is there way to do like that?

Yeah, sure. Here's one way:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string randomChar = "abcdefghijklmnopqrstuvwxyx";
srand( (unsigned int)time( 0 ) );
// Let's get 20 random somethings
for ( int i = 0; i < 20; i++ ) {
// 50% probability for either a letter or a number
if ( rand() < RAND_MAX / 2 )
cout << rand() % 10 + 1 << '\n';
else
cout << randomChar[rand() % randomChar.length()] << '\n';
}
return 0;
}

This is a cool way to do it because you can change the weight that letters or numbers have. Right now it's with equal probability across the board, but you could change the test to RAND_MAX / 3 and have numbers 1/3 of the time and letters 2/3 of the time. That's better than just rand because rand uses a uniform distribution and everything has equal probability.

You really can't get both letters and numbers at the same time. You can fake it if the numbers only have one digit by getting the character representation of numbers. That's another way to do it, but you have more restrictions.

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string randomChar = "0123456789abcdefghijklmnopqrstuvwxyx";
srand( (unsigned int)time( 0 ) );
// Let's get 20 random somethings
for ( int i = 0; i < 20; i++ )
cout << randomChar[rand() % randomChar.length()] << '\n';
return 0;
}

There's tons of ways to do what you want, so play around with some of this stuff and have fun! :)

you could do a rand()%36, take 0-25 as letters and 26-35 as numbers (subtract 25 from them if you want values in [1,10]). Probably use an if to determine if it's a letter or number, since the letters and numbers need to be adjusted ( theValue + 'a' for letters, numbers described above).