regarding random function

This is a discussion on regarding random function within the C++ Programming forums, part of the General Programming Boards category; i knw that random function random(n) produces random number from 0 to n-1 and through dis i can i easily ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    11

    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. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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';
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    thnks MK27
    so likewise we can do for string containing numbers and alphabets

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    11
    Quote Originally Posted by MK27 View Post
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ratikag View Post
    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".

    [edit] 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.
    Last edited by MK27; 03-05-2010 at 10:09 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    O_o

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

    Soma

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    O_o

    You have a mistake.
    Nope -- unless you meant the "and/add" typo which was, contextually, obviously a typo.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    *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.)

    [Edit]

    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. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    Your code has a bug. (A bug that was described to you in detail.)
    Whoops! It's the idea that counts

    Quote Originally Posted by phantomotap View Post
    I really need to find a new forum...
    Don't be silly -- look how helpful you've been here.
    Last edited by MK27; 03-05-2010 at 10:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    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 subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21