Generate random value with formatting

Asked

Viewed 170 times

3

How can I generate a random string with a certain format? I’m doing it with PHP encryption but I can’t format the values.

I wanted it to generate according to the following example:

A9MDY3-X6S

My code is only creating one md5 but wanted to generate a text as in the example above.

My code:

<?php md5(date("d/m/Y H:i:s")); ?>

Note: I would like to perform in Javascript.

3 answers

4


hello, you can use a function like this:

    function gerarCombinacao() {

        $combinacao = chr(rand(97,122));
        $combinacao .= chr(rand(97,122));
        $combinacao .= chr(rand(97,122));
        $combinacao .= "-";
        $combinacao .= rand(0,9);
        $combinacao .= rand(0,9);
        $combinacao .= rand(0,9);

        return $combinacao; 
    }

The result of the above function will be; Asd-962

where it will generate string with random numbers of 0-9 and letters of a-z

The character types within the CHR can be checked at https://www.ascii-code.com/

2

With php:

<?php
$hash = strtoupper(md5(date("d/m/Y H:i:s")));
$hash = substr($hash,0,9); //Corta a string para 9 digitos;
$hash = substr_replace($hash, '-', 6, 0 ); //Insere o sinal na posicao indicada;
echo $hash;

With JavaScript:

    function randomString() {
    	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    	var length = 9;
    	var randomstring = '';
    	for (var i=0; i < length; i++) {
    		var rnum = Math.floor(Math.random() * chars.length);
    		randomstring += chars.substring(rnum, rnum + 1);
    	}
    	var output = [randomstring.slice(0, 6), '-', randomstring.slice(6)].join('');
    	document.randform.randomfield.value = output;
    }
<form name="randform">
<input type="button" value="Create Random String" onClick="randomString();">&nbsp;
<input type="text" name="randomfield" value="">
</form>

Sources: How to Create Random Numbers & Characters, Inserting string at position x of Another string.

1

    function gerarNumeroAleatorio() {
    
      var texto = "";
      var valores= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      var tamanho = 9;
    
      for (var i = 0; i < tamanho; i++)
        texto += valores.charAt(Math.floor(Math.random() * valores.length));
    
      console.log(texto.substr(0,6) + "-" + texto.substr(6,10));
      
      }
    
    <button onClick="gerarNumeroAleatorio();">GERAR</button>
    
  

I wanted it to generate according to the following example: A9MDY3-X6S.

What I took into consideration was to generate a String with random values mixing alphabetic and numerical values.

The variable valores, has all the diversity of possible random values that we should "draw". The variable size(9) refers to the number of characters we will have in the final value (consider the starting point as the index 0). And made a loop and inside it the random numbers/character separated by a hyphenate in the following order: 6 dígitos(texto.substr(0,6) - 3 dígitos(texto.substr(6,10).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.