简体   繁体   中英

How to generate an N digit random number using string as seed in Perl

I am looking for a way to generate a 4 digit random number using the file path as input. I am trying to give unique name to each software installation in my machine and the name is software_product_name + 4 digit random number . It's very rare to have the same software installed twice but if it is the case I don't want to end up with the same name. I have a different way to get the software name.

Is there an efficient way I can generate 4 digit random number that would result in different number for different paths?

#!/usr/bin/perl

print "Content-type: text/html\n\n";

my @Chars = ('1'..'9');
my $Length = 4;
my $Number = '';

for (1..$Length) {
$Number .= $Chars[int rand @Chars];
}

print $Number;

If you think about it, you can't do this and you shouldn't be doing this. You want to generate something without collisions so you can distinguish between different installations. You're not going to be able to distinguish an infinite number of possible paths with a four digit number.

It sounds like your software needs to be smart enough not to blow away an existing directory and to install in a user-defined path.

Alternatively, you just generate any random number and check that it's not already in use. So you don't care what that number actually is, you don't need any input to generate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM