randomize character

Discussion in 'C++' started by ashu, Oct 18, 2005.

  1. ashu

    ashu Guest

    can any one tell me that is there any way to get a randomize (genuine)
    character like integer. for integer, we use random function. & for
    character, we use ????
     
    ashu, Oct 18, 2005
    #1
    1. Advertising

  2. ashu

    mlimber Guest

    ashu wrote:
    > can any one tell me that is there any way to get a randomize (genuine)
    > character like integer. for integer, we use random function. & for
    > character, we use ????


    The same function. Just keep 8-bits (or whatever) worth. For instance:

    const int ri = // ... compute random int ...
    const char rc = ri & 0xff; // 8-bit char

    Cheers! --M
     
    mlimber, Oct 18, 2005
    #2
    1. Advertising

  3. ashu

    ashu Guest

    thank you mlimber, but i want to use this randomly generated character
    to name(atleast 3-4 characters) as file (to create a file). so is there
    any possible way to get that, may be by some loop.
    please answer in source code as you did earlier.
     
    ashu, Oct 18, 2005
    #3
  4. mlimber wrote:
    > ashu wrote:
    >
    >>can any one tell me that is there any way to get a randomize (genuine)
    >>character like integer. for integer, we use random function. & for
    >>character, we use ????

    >
    >
    > The same function. Just keep 8-bits (or whatever) worth. For instance:
    >
    > const int ri = // ... compute random int ...
    > const char rc = ri & 0xff; // 8-bit char


    Bad idea. Read the C FAQ to see why & (just like %) is a bad idea to
    be used with the result of calling rand().

    V
     
    Victor Bazarov, Oct 18, 2005
    #4
  5. ashu

    mlimber Guest

    Victor Bazarov wrote:
    > mlimber wrote:
    > > ashu wrote:
    > >
    > >>can any one tell me that is there any way to get a randomize (genuine)
    > >>character like integer. for integer, we use random function. & for
    > >>character, we use ????

    > >
    > >
    > > The same function. Just keep 8-bits (or whatever) worth. For instance:
    > >
    > > const int ri = // ... compute random int ...
    > > const char rc = ri & 0xff; // 8-bit char

    >
    > Bad idea. Read the C FAQ to see why & (just like %) is a bad idea to
    > be used with the result of calling rand().


    Good catch, Victor. The C FAQ
    (http://www.faqs.org/faqs/C-faq/faq/index.html) suggests this sort of
    thing instead:

    const char rc = char( std::rand() / (RAND_MAX / N + 1) );

    where RAND_MAX is defined in <cstdlib> and N is 256 for 8-bit
    characters. So, to get several random characters, just put that
    expression in a loop, and voila! If you need more sophistication in
    random number generation, see the Boost Random Number Library
    (http://boost.org/libs/random/index.html).

    Cheers! --M
     
    mlimber, Oct 18, 2005
    #5
  6. ashu

    mlimber Guest

    ashu wrote:
    > thank you mlimber, but i want to use this randomly generated character
    > to name(atleast 3-4 characters) as file (to create a file). so is there
    > any possible way to get that, may be by some loop.
    > please answer in source code as you did earlier.


    Sure, but we're not going to do your homework for you. See my other
    posts, try it, and ask a specific question if you have a problem.

    (BTW, there's also the yucky old std::tmpfile function
    [http://www.cplusplus.com/ref/cstdio/tmpfile.html] for C-style FILE
    streams, which may do what I suspect you really want. Unfortunately, it
    is only for C-style code, and there's no C++ or Boost equivalent that I
    am aware of.)

    Cheers! --M
     
    mlimber, Oct 18, 2005
    #6
  7. ashu

    Default User Guest

    ashu wrote:

    > thank you mlimber, but i want to use this randomly generated character
    > to name(atleast 3-4 characters) as file (to create a file). so is
    > there any possible way to get that, may be by some loop.
    > please answer in source code as you did earlier.


    So, you have a much more complicated problem than you first described.
    Needing four characters is minor. You need to construct a legal file
    name on your particular platform. That means you have to determine what
    constitutes legal characters.

    For instance, are punctuating characters legal? All of them? Some of
    them? Allowed as the first character? Are numbers legal in the first
    spot? Are capitals distinct from lowercase?

    I'd make up an arrays of legal characters and generate random indexes
    into it to get the characters.

    Of course, this in no way guarantees unique file names. It would be
    good to give us the full problem description as well.

    Also, read my .sig below.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Badass Scotsman

    Why wont this Randomize? (Classic VB Script ASP)

    Badass Scotsman, May 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    517
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    May 5, 2006
  2. Zaphod

    Randomize STL stack

    Zaphod, Apr 25, 2004, in forum: C++
    Replies:
    3
    Views:
    690
    Andrew Koenig
    Apr 27, 2004
  3. Sweety

    own code for randomize

    Sweety, Jul 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    303
    Erik de Castro Lopo
    Jul 27, 2004
  4. Mr. x

    randomize function

    Mr. x, Sep 18, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    105
    Mr. x
    Sep 18, 2003
  5. Replies:
    3
    Views:
    77
Loading...

Share This Page