473,394 Members | 1,781 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,394 software developers and data experts.

Generate random names

Srijith B
HI all
I am trying to write a perl script to generate random names ed. ( Qrhfh, Jdfhdh, Dfkjns) etc.

So the script should ask me the number of random names and when I give say 4, It should give me four random names and then it should insert these names into the sql database to a table which is already created.

Here is my code.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my $name;
  3. my @charset = (('A'..'Z'), ('a'..'z'));
  4. my $range = $#charset + 1;
  5. print "Enter the limit\n";
  6. $lim = <STDIN>;
  7.  
  8. until ($lim == 0){
  9. for (1..8) {
  10.    $name .= $charset[int(rand($range))];
  11. }
  12. print "$name\n";
  13. $lim = $lim - 1;
  14. }
  15.  
I have a problem in this code,
say when I run this script and enter 6

It gives me the following weird output.

wFposZwA
wFposZwAHnAEHiei
wFposZwAHnAEHieiJLNQZiTW
wFposZwAHnAEHieiJLNQZiTWzxOUiiru
wFposZwAHnAEHieiJLNQZiTWzxOUiiruHdinQLkp
wFposZwAHnAEHieiJLNQZiTWzxOUiiruHdinQLkpdLwNtOYW

I dont want it to have same prefix in the front and secondly I don't want its length to increase.

Anyone got a clue wats happening ?
Apr 8 '11 #1
1 4697
miller
1,089 Expert 1GB
Always limit the scope of your variables to the smallest space available. Also add use strict; and use warnings; to the beginning of all scripts:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my @charset = ('A'..'Z', 'a'..'z');
  7.  
  8. print "Enter the limit\n";
  9. chomp(my $lim = <STDIN>);
  10.  
  11. for (1..$lim) {
  12.     my $name = join '', @charset[map {int rand @charset} (1..8)];
  13.     print "$name\n";
  14. }
  15.  
Apr 8 '11 #2

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

Similar topics

5
by: Yudan YI \(OSU\) | last post by:
I want to generate random data from a normal distribution, while I checked the functions, and I found rand(), which returns a pseudorandom integer between zero and RAND_MAX. I am not sure how to...
4
by: Marc Dansereau | last post by:
Hi all, I wonder what is the most efficient way to generate random point on a line defined by 2 double points (x0,y0) and (x1,y1). Here is the pseudocode of my method : for each point {...
7
by: MattB | last post by:
I have the following code to generate random passwords for new users of an application. Const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstyvwxyz1234567890" Dim r As Int16, i As...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
1
by: venkatcute | last post by:
Dear Members, I have just started programming java. My teacher gave me an assignment where i have to create an array of objects of arbitrary length ( length decided by the user). Each object...
0
sashi
by: sashi | last post by:
Generate Random Password In the course of programming you may have cause to generate a password. The following function will generate a password of randomly selected characters up to a maximum of...
3
by: jjmontreal | last post by:
I would like to create a form where by clicking some where in the form it will generate a alphanumeric sequence. I want to generate random PIN numbers preceeded with specific letters. An example...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
4
by: pitchke | last post by:
how do i generate random no's...also i want to convert that no. to the corresponding string...using PHP!!!!! thanks in advance.. Tarun
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
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...
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...

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.