mercredi 11 septembre 2019

How to generate fixed length random number without conflict?

I'm working on an application where I've to generate code like Google classroom. When a user creates a class I generate code using following functions

 private String codeGenerator(){
        StringBuilder stringBuilder=new StringBuilder();
        String chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        int characterLength=chars.length();
        for(int i=0;i<5;i++){
            stringBuilder.append(chars.charAt((int)Math.floor(Math.random()*characterLength)));
        }
        return stringBuilder.toString();
    }

As I have 62 different characters. I can generate total 5^62 code total which is quite large. I can generate this code in server or user device. So my question is which one is better approach? How likely a generated code will conflict with another code?




Aucun commentaire:

Enregistrer un commentaire