473,835 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How would you create a random string of data

3 New Member
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 6401
9,065 Recognized Expert Moderator Expert
Here you go

"ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz0123456 789AEIOUaeiou76 54321"
Jul 26 '06 #2
32 New Member
That is a string generator only for characters...

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

printf("Value: ");
scanf("%d", &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
9,065 Recognized Expert Moderator Expert
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;
  9. printf("Value: ");
  10. scanf("%d", &val);
  12. srand(val);
  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. }
  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. */
Jul 27 '06 #4

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

Similar topics

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 to create some plain text data that ZIP cannot compress anymore? any algorithm? String str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; StringBuffer buf = new StringBuffer(); for (int i=0; i<1024; i++) {
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 insert it automatically?
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 difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
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 cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
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?
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
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 of values. I get a random index value for each array so I can pull the data from them.
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 generally make use of DW's builtin commands and some extensions. Anyway I'm creating a registration site for event exhibitors and I've been asked to come up with a method of automatically generating passwords for inserted records, either as...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.