Skip to content

Instantly share code, notes, and snippets.

@iampato
Created December 9, 2022 12:49
Show Gist options
  • Select an option

  • Save iampato/72cfd7fc0f5a4b307ca5474aa94aca9c to your computer and use it in GitHub Desktop.

Select an option

Save iampato/72cfd7fc0f5a4b307ca5474aa94aca9c to your computer and use it in GitHub Desktop.
Here is a possible solution for a Dart function that randomly generates unique names without using predefined lists of names:
import 'dart:math';
// A set to store the generated names.
final Set<String> generatedNames = Set();
String generateUniqueName() {
// Generate a random first and last name.
final firstName = generateRandomName();
final lastName = generateRandomName();
// Concatenate the first and last name to form a full name.
final name = "$firstName $lastName";
// If the name is not in the set of generated names, add it and return it.
// Otherwise, generate a new name.
if (!generatedNames.contains(name)) {
generatedNames.add(name);
return name;
} else {
return generateUniqueName();
}
}
// Generates a random name by picking a random letter from the alphabet
// and repeating it a random number of times (between 2 and 6).
String generateRandomName() {
final random = Random();
final charCode = random.nextInt(26) + 97; // Generate a random letter from 'a' to 'z'.
final char = String.fromCharCode(charCode);
final length = random.nextInt(5) + 2; // Generate a random length between 2 and 6.
return char * length; // Repeat the letter the given number of times.
}
@iampato

iampato commented Dec 9, 2022

Copy link
Copy Markdown
Author

Screenshot 2022-12-09 at 15 50 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment