Not Yet Answered # random

Discussion Starter sTorM Ancient Dragon 5,243 Ravalon 62 Infarction 503 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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:

0

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.

0

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! :)

0

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).

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...