# Thread: regarding random function

1. ## regarding random function

i knw that random function random(n) produces random number from 0 to n-1 and through dis i can i easily get d random alphabets but if i want to get alphabets in both caps like "AsdRTuhGnK" den how would i break the parameters

i know to get the large caps the code is

char ch=random(26)+'A';

and if i write
char ch=random(57)+'A'
then it would give 6 special symbols too
so is there any solution for it other then to test those six ascii value and to put some alphabets in place of it?

2. Since A-Z is not contiguous with a-z in the ascii table
Code:
```int x = rand()%52;  // 0-51
if (x < 26) x += 'A';
else x += 'a';```

3. thnks MK27
so likewise we can do for string containing numbers and alphabets

4. Originally Posted by MK27
Since A-Z is not contiguous with a-z in the ascii table
Code:
```int x = rand()%52;  // 0-51
if (x < 26) x += 'A';
else x += 'a';```
hey there is lil mistake in else x+='a'
it would be

else x+=71;

bcoz when x=26
then 26+71=97 which is a ascii code for "a"
but if u add x+='a' then when x=26
x=26+97=123 which is a ascii code for"{"
so it would be else x+=71;
or else x+='G';

5. Originally Posted by ratikag
so likewise we can do for string containing numbers and alphabets
Speaking of a string, one option is to define a string that contains the possible characters to choose from, then generate a (pseudo)random number that is a valid index of the string, and thus get the corresponding randomly selected character.

6. Originally Posted by ratikag
hey there is lil mistake in else x+='a'
No, there is not if you consider 0-25 to be 'A-Z' and 26-52 to be 'a-z'. If the result is 1, you add 'A' to get "B", if the result is 27 you add 'a' to get "b".

 Sorry you are right, should've been
Code:
`else x += 'a'-26;`

Laserlight's idea is a good one esp. if you want to include punctuation, etc.

7. O_o

You have a mistake. It isn't a big deal, but your English description doesn't match your source implementation.

Soma

8. Originally Posted by phantomotap
O_o

You have a mistake.
Nope -- unless you meant the "and/add" typo which was, contextually, obviously a typo.

9. *shrug*

I really need to find a new forum...

Your English description is correct. (I'm not talking about your latest description either.) Your code has a bug. (A bug that was described to you in detail.)

I'll just save us both the trouble. Is `original' really producing correct results relating to your goal?

[/Edit]

Soma

Code:
```#include <cstdlib>
#include <iostream>

char original
(
unsigned int x
)
{
// unchanged
if (x < 26) x += 'A';
else x += 'a';
return(char(x));
}

char correct
(
unsigned int value_f
)
{
if(26 > value_f)
{
return(char(value_f + 'A'));
}
else
{
return(char(value_f + 'a' - 26));
}
}

int main()
{
for(int x(0); 52 > x; ++x)
{
std::cout << correct(x) << ':' << original(x) << '\n';
}
return(0);
}```

10. Originally Posted by phantomotap
Your code has a bug. (A bug that was described to you in detail.)
Whoops! It's the idea that counts

Originally Posted by phantomotap
I really need to find a new forum...
Don't be silly -- look how helpful you've been here.

11. Don't be silly -- look how helpful you've been here.
O_o

Then the forum software should stop eating my posts.

Soma

Popular pages Recent additions