randomize character

A

ashu

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 ????
 
M

mlimber

ashu said:
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
 
A

ashu

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

Victor Bazarov

mlimber said:
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
 
M

mlimber

Victor said:
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
 
M

mlimber

ashu said:
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
 
D

Default User

ashu said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top