I have this code for generating random strings
public function random_string($length = 5)
{
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
return substr(str_shuffle($chars),0,$length);
}
So, is it possible that two generated strings can be same ?
In my case, there can be a maximum of 62P5 (using permutation) numbers of strings of 5 characters.
But whats the possibility that the 10th & 1000th generated random strings be same ?
This is known as the birthday problem and can be solved by
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
$length = 5;
$numChars = strlen($chars);
$numberOfStringsGenerated = 50000;
print "numStringsGenerated: " . $numberOfStringsGenerated . "\n";
print "numChars: " . $numChars . "\n";
print "lengthOfString: " . $length . "\n";
$totalPerms = 1;
for ($ii=0; $ii<$length; $ii++) {
$totalPerms *= $numChars - $ii;
}
print "totalPerms: " . $totalPerms . "\n";
$totalProbablity = 1;
for ($ii=0; $ii< $numberOfStringsGenerated; $ii++) {
$totalProbablity *= ($totalPerms - $ii)/$totalPerms;
}
print "Probablity: ";
print 1 - $totalProbablity . "\n";
Here is the codepad output
Here is the Wikipedia page
This calculation assumes that the PRNG for the str_shuffle is good enough for all permutations to be equally likely, which won't be exactly true, especially as the number of chars increases.
Obviously that is possible.
The right way to do this, is store already used strings in a database.
I use this in a system that generates a random session token that is stored at database.
Always that I generate a new random session token, i query for it in session table. If no results, OK, else, generate a new token.
The probability is very low, but not impossible, and everytime you insert new tokens at table, the probability of overwriting just grow.
Related
I have tried to use say-as interpret-as to make Alexa speak number in digits
Example - 9822 must not read in words instead '9,8,2,2'
One of the two ways I have tried is as follows:
this.emit(':tell',"Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>",'backup');
The other one is this:
this.response.speak("Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>");
Both are not working but working on a separate fresh function.
Actually your code SHOULD work.
Maybe you can try in test simulator and send us the code your script produces? Or the logs?
I've tried the following:
<speak>
1. The numbers are: <say-as interpret-as="digits">5498</say-as>.
2. The numbers are: <say-as interpret-as="spell-out">5498</say-as>.
3. The numbers are: <say-as interpret-as="characters">5498</say-as>.
4. The numbers are: <prosody rate="x-slow"><say-as interpret-as="digits">5498</say-as></prosody>.
5. The number is: 5498.
</speak>
Digits, Spell-out and Characters all have the effect you want.
If you want to Alexa to say it extra slow, use the prosody in #4.
Try using examples #2 or #3, maybe this works out?
Otherwise the example from Amod will work too.
You can split number into individual digits using sample function ( please test it for your possible inputs-its not tested for all input). You can search for similar function on stackoverflow
function getNumber(tablenumber) {
var number = (""+tablenumber).split("");
var arrayLength = number.length;
var tmp =" ";
for (var i = 0; i < arrayLength; i++) {
var tmp = tmp + myStringArray[i] + ", <break time=\"0.4s\"/> ";
}
return tmp;
}
In your main function... call this
var finalresult = getNumber(clientno);
this.emit(':tell',"Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ finalresult ,'backup');
Edited: Yep, nightflash's answer is great.
You could also break the numbers up yourself if you need other formatting, such as emphasizing particular digits, add pauses, etc. You would need to use your Lambda code to convert the numeric string to multiple digits separated by spaces and any other formatting you need.
Here's an example based on the answers in this post:
var inputNumber = 12354987;
var output = '';
var sNumber = inputNumber.toString();
for (var i = 0, len = sNumber.length; i < len; i += 1) {
// just adding spaces here, but could be SSML attributes, etc.
output = output + sNumber.charAt(i) + ' ';
}
console.log(output);
This code could be refactored and done many other ways, but I think this is about the easiest to understand.
Right now i have to write a code that will print out "*" for each collectedDots, however if it doesn't collect any collectedDots==0 then it print out "". Using too many if statements look messy and i was wandering how you would implement the forloop in this case.
As a general principle the kind of rearrangement you've done here is good. You have found a way to express the rule in a general way rather than as a sequence of special cases. This is much easier to reason about and to check, and it's obviously extensible to cases where you have more than 3 dots.
You probably have made an error in confusing your target number and the iteration value, I assume that collectedDots contains the number of dots you have (as per your if statement) and so you need to introduce a variable to count up to that value
for (int i =0; i <= collectedDots; i++)
{
stars = "*";
System.out.print(stars);
}
Ok, so you already have a variable called collectedDots that is a number which tells you how many stars to print?
So your loop would be something like
for every collected dot
print *
But you can't just print it out, you need to return a string that will be printed out. So it's more like
for every collected dot
add a * to our string
return the string
They key difference between this and your attempt so far is that you were assigning a star to be your string each time through the loop, then at the end of it, you return that string–no matter how many times you assign a star to the string, the string will always just be one star.
You also need a separate variable to keep track of your loop, this should do the trick:
String stars = "";
for(int i = 0; i < collectedDots; i++)
{
stars = stars + "*";
}
return stars;
You are almost correct. Just need to change range limit of looping. Looping initial value is set to 1. So whenever you have collectedDots = 0, it will not go in loop and will return "", as stars is intialized with "" before loop.
String stars = "";
for (int i =1; i <= collectedDots; i++)
{
stars = "*";
System.out.print(stars);
}
return stars;
Afternoon,
I have an odd algorithm. I would like to populate a string of code dynamically based on some user entry.
I have a multi-dimensional array with data in it and a multi-line input text field.
What I want is for a user to be able to enter some text
example:
00
01 - 02 - 03
comments: 12
my code would identify the numbers an treat everything else as text.
Thus, if my array is data[x][#], the # will correspond to their entry.
I would get
algorithm_string = data[x][0] + "\n" + data[x][1] + " - " + data[x][2] + " - " + data[x][3] + "\n" + "comments: " + data[x][12]
So the algorithm would construct the above, and then I could run through the code.
for(var x:int = 0; x < data.length; x++){
some_object._display_text.text = algorithm_string;
}
Ok so I want to first say that relying on a user to put in the entry exactly the way you want is probably not a good idea. They WILL make mistakes and your code WILL eventually not work as expected. I would recommend using 5 inputs restricted to numeric input, and labeling each field with which number should go in it.
However, you can accomplish what you are trying to do above like this:
var parts:Array = myInput.text.split(" ");
for (var i:int=0; i<parts.length, i++){
if(!isNaN(parseInt(parts[i]))){
// you have a number here.
data[x].push(parts[i]);
} else {
//this was not a number so ignore it
}
}
Again let me state I think you should refactor how you get the numbers, but that code will grab the numbers out and put them in the 0,1,2,3,and 4 indexes of your data[x], but relies on the user perfectly inputting the text every time.
Good luck! (refactor) :)
I run perforce command on a list of files and after some parsing and stuff i generate a file that contains owners like this(call it owner.log):
ownerA
ownerB
ownerC
ownerA
ownerA
then i go throug the owner.log file and pick an owner like this:
while(<OWNER>) {
$vote->{$_} += 1;
}
and then the owner with the highest vote gets selected for email notification. But the problem is when i have an owner log like this:
ownerA
ownerB
ownerC
ownerD
each one gets the same vote? How should i pick one?
Thank you.
Is there a quick way of finding if all hashes have same value? that way i can pick one at random.
One way to determine if all hash keys have the same value is to use uniq. If there is only one common value, use the keys of your hash as an array and use rand to find a random index within the array bounds:
use More::ListUtils qw(uniq);
my #keys = keys %hash;
my #vals = values %hash;
if (scalar uniq(#vals) == 1) {
print "all of equal weight\n";
print $keys[ int(rand(#keys)) ], "\n";
}
Assuming the array #winners:
print "The winner is: ", $winners[rand #winners];
The whole process:
my $last = 0;
my #winners;
for my $name (sort { $vote->{$b} <=> $vote->{$a} } keys %$vote) {
last if ($vote->{$name} < $last);
push #winners, $name;
$last = $vote->{$name};
}
my $winner = $winners[rand #winners];
print "The winner is, by ",
#winners == 1 ? "unanimous vote: " : "luck of the draw: ", $winner;
I'm making a script to create a username. It should be four letters long; traditionally we've used 3 letters of the last name + 1 of the first name.
If it was already used we manually thought of an alternative.
So if my name is Fred Flinstones we should try FLIF. If this doesnt work; we loop through the name: FLIA, FLIB, FLIC ... FLIZ, FLAA, FLAB, FLAC, ... FLZZ, FAAA, FAAB, ...
The easiest way is to loop through last letters; then make another set of loops through second last letter and loop through last letters; then a set of loops through third last, second last, last; and fourth+third+second+last.
This makes a lot of do while loops nested in eachother + unreadable for other humans + a lot of typing.
I could use a counter per letter but that also doesn't seem elegant
I could try with one counter and then using mod 26 to see how many letters need replacement (but that seems very complex).
Is there some elegant/efficient ways to do this?
Bonus points for first trying to keep the string as 'logically correct' as possible (f.e. keeping the last letter an F for Fred or skipping letters FLIF; FLNF, FLSF, FLTF, ...) .
Not sure if this is what you mean, but if you structure your username-script in the following way (I used PHP as language), you can extend it by adding options with higher fuzz factors while keeping the code readable:
echo findName('FLINTSTONE', 'FRED');
function findName($last, $first) {
for ($fuzzFactor = 0; ; $fuzzFactor++) {
$candidates = fuzzNames($last, $first, $fuzzFactor);
if (empty($candidates)) {
// exhausted
return "sorry, I'm out of options!";
}
foreach ($candidates as $candidate) {
if (isUnique($candidate)) {
return $candidate;
}
}
}
}
function fuzzNames($last, $first, $fuzzFactor) {
switch ($fuzzFactor) {
case 0:
// no fuzz, return first choice
return array(substr($last, 0, 3) . $first[0]);
case 1:
// replace the third letter of the last name
// by the fourth/fifth/.../last letter (FLNF, FLTF, ... , FLEF)
$candidates = array();
for ($i = 3; $i < strlen($last); $i++) {
$candidates[] = substr($last, 0, 2) . $last[$i] . $first[0];
}
return $candidates;
case 2:
// replace the second and third letter of the last name
// by their follow-ups (FINF, FITF, ... , FNEF)
$candidates = array();
for ($i = 2; $i < strlen($last) - 1; $i++) {
for ($j = $i + 1; $j < strlen($last); $j++) {
$candidates[] = $last[0] . $last[$i] . $last[$j] . $first[0];
}
}
return $candidates;
default:
return array();
}
}