473,414 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How would you create a random string of data

I have a program that takes data entered from a keyboard and uses it as a random seed value, is there anyway to generate a string of up to 80 characters that could be used in place of data entered from a keyboard?
Jul 26 '06 #1
3 6385
Banfa
9,065 Expert Mod 8TB
Here you go

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz0123456789AEIOUaeiou7654321"
Jul 26 '06 #2
axas
32
That is a string generator only for characters...

char s[80];
int i, val;
int ch1, ch2, num;

printf("Value: ");
scanf("%d", &val);

srand(val);

for (i = 0; i < strlen(s); i++) {
s[i] = rand() % 'z' + 'A';
if (s[i] > 'Z')
s[i] = 'A';
if (s[i] < 'A')
s[i] = 'Z';
}

s[i] = '\0';
Jul 27 '06 #3
Banfa
9,065 Expert Mod 8TB
That is a string generator only for characters...
Nice try but you have some errors

Expand|Select|Wrap|Line Numbers
  1. char s[80];
  2. /* We want a string of 80 characters.  Since it's a string 
  3.    there will need to be a NULL terminator (which you add).  
  4.    However that makes 80 characters + 1 NULL terminator = 81 
  5.    char required, your array is 1 char too small. */
  6. int i, val;
  7. int ch1, ch2, num;
  8.  
  9. printf("Value: ");
  10. scanf("%d", &val);
  11.  
  12. srand(val);
  13.  
  14. for (i = 0; i < strlen(s); i++) {
  15. /* Unfortunately s has not been initialised at this point 
  16.    so strlen(s) could return anything between 0 and the 
  17.    maximum value that size_t can hold (a very large number).  
  18.    I suggest sizeof s would be better here */
  19.     s[i] = rand() % 'z' + 'A';
  20. /* The low bits of the return value of rand() can be not 
  21.    very random so rand() % anything small is not very random.  
  22.    Also this expression results in a value between 
  23.    'A' and 'z' + 'A'  - 1 but imediately below any 
  24.    value > 'Z' is set to 'A'.  This will result in a more than 
  25.    50% chance that any character is 'A', not very random. */
  26.     if (s[i] > 'Z') 
  27.         s[i] = 'A';
  28.     if (s[i] < 'A') 
  29.         s[i] = 'Z';
  30. }
  31.  
  32. s[i] = '\0';
  33. /* Assuming you meant sizeof at this point i would be 
  34.    81 and you would be writing outside the array bounds 
  35.    and invoking undefined behaviour. */
  36.  
Jul 27 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Roomates Computer | last post by:
I have the following code, and I use this method to create a 1 MB file by running it 1024 times. After it writes the file, I tried to use ZIP to compress it, the file size is down to 50%!! How...
2
by: Shapper | last post by:
Hello, I need to create a new record in a database. The database has 3 fields: (autonumber), and (strings) When I create the record how should I create the value? Does the database...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
7
by: tamsun | last post by:
we need create a GUID in web page, just like: {26C2E7C8-C689-D1D5-C452-58EC5A2F2A39} Could anyone tell me how to use javascript to create it without ActiveX object?
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
7
by: wozza | last post by:
hi I'm a Dreamweaver user who's created a few simple data entry/ registrations forms in my time, but I'm not really a coder (though I can follow instructions and am not afraid to dabble...) - I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.