Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 05-22-2004, 02:12 PM   #1
Registered: May 2003
Posts: 256

Rep: Reputation: 30
Question Produce random character with c program

I need some advice on random characters.

Last edited by liguorir; 05-23-2004 at 07:27 PM.
Old 05-22-2004, 03:19 PM   #2
LQ Newbie
Registered: May 2004
Distribution: Red Hat 9
Posts: 20

Rep: Reputation: 0

When you initiate the rand function with randomize (in stdlib) it uses the current time / date to make the number random. If you use srand to initiate the number, you would need to write something like srand((NULL)time())...
To produce the random char I would do something like this:

char c[] = {"abcde.....ABCDE.....?.,:$!"0123456789"} // all valid chars
int n = strlen(c); // #chars in c[]
int i = rand(); // a random number from 0 to 32767
i = i % n; // i will now be in the interval from 0 to n-1
c[i]; // will be a random char

However, it is possible to improve the quality of the random number by replacing
i = rand();
do { i = rand(); } while (i >= n * (int)(32768 / n)) ;

Hope this helps.
Old 05-22-2004, 03:26 PM   #3
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
if you want a random ASCII char, i believe
char chr = (char) i
will work, where i is from 0 to 255

also, shouldn't this be in the programming forum?
Old 05-23-2004, 08:57 AM   #4
LQ Newbie
Registered: May 2004
Posts: 18

Rep: Reputation: 0
Here are some source codes

Here are some source codes
Old 05-23-2004, 06:30 PM   #5
Registered: May 2003
Posts: 256

Original Poster
Rep: Reputation: 30

Thanks for the advise

Last edited by liguorir; 05-23-2004 at 07:28 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random program crashes in Linux, is fedora not stable? xbennyboy Linux - Newbie 13 08-05-2004 11:15 AM
How to produce two binary program using automake melinda_sayang Programming 1 06-05-2004 11:30 AM
Running a program inside my QT application does not produce needed output files??? keos Programming 0 02-22-2004 11:37 PM
Generating random numbers for C program J-Stew Programming 14 02-06-2004 04:49 AM
Random program termination - odd jamespetts Linux - General 4 11-01-2002 04:55 PM

All times are GMT -5. The time now is 07:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration