Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > randomize character

Reply
Thread Tools

randomize character

 
 
ashu
Guest
Posts: n/a
 
      10-18-2005
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 ????

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-18-2005
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

 
Reply With Quote
 
 
 
 
ashu
Guest
Posts: n/a
 
      10-18-2005
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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-18-2005
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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-18-2005

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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-18-2005
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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-18-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux software to randomize PC hostname & wireless MAC for privacyat hotspots Y Knot Computer Security 13 10-01-2010 08:44 PM
Why wont this Randomize? (Classic VB Script ASP) Badass Scotsman ASP .Net 1 05-05-2006 07:53 PM
own code for randomize Sweety C Programming 2 07-27-2004 08:28 AM
Randomize STL stack Zaphod C++ 3 04-27-2004 02:55 PM
randomize function Mr. x ASP .Net Web Services 0 09-18-2003 05:38 PM



Advertisments