Skip to content
Fix Code Error

How can I generate random alphanumeric strings?

March 13, 2021 by Code Error
Posted By: Anonymous

How can I generate a random 8 character alphanumeric string in C#?

Solution

I heard LINQ is the new black, so here’s my attempt using LINQ:

private static Random random = new Random();
public static string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}

(Note: The use of the Random class makes this unsuitable for anything security related, such as creating passwords or tokens. Use the RNGCryptoServiceProvider class if you need a strong random number generator.)

Answered By: Anonymous

Related Articles

  • The definitive guide to form-based website authentication
  • Fastest way to iterate over all the chars in a String
  • How to generate a random alpha-numeric string
  • Ubuntu apt-get unable to fetch packages
  • Problems Installing CRA & NextJS from NPM…
  • Is it possible to apply CSS to half of a character?
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • TypeScript metadata reflection references other…
  • How Spring Security Filter Chain works
  • Generating a random & unique 8 character string…
  • Secure hash and salt for PHP passwords
  • Callback functions in C++
  • How to filter a RecyclerView with a SearchView
  • Where does Internet Explorer store saved passwords?
  • Remix error The transaction has been reverted to the…
  • Requested bean is currently in creation: Is there an…
  • Draw in Canvas by finger, Android
  • What is and how to fix…
  • What are the undocumented features and limitations…
  • Random alpha-numeric string in JavaScript?
  • useEffect Error: Minified React error #321 (GTM…
  • Adding gif image in an ImageView in android
  • How to fix Hibernate LazyInitializationException:…
  • Safe truncate string contains color tag
  • Accessing a Shared File (UNC) From a Remote,…
  • I want to create a SQLite database like in the…
  • Random String Generator Returning Same String
  • Javax.net.ssl.SSLHandshakeException:…
  • How to make Javascript font responsive?
  • Multipart File Upload Using Spring Rest Template +…
  • An Authentication object was not found in the…
  • Auto-fit TextView for Android
  • Builder pattern without inner class
  • Why is 2 * (i * i) faster than 2 * i * i in Java?
  • How to generate a random string of a fixed length in Go?
  • How to get the promise value in reactjs?
  • How to disable SSL certificate checking with Spring…
  • TypeScript DefinitelyTyped Ember.js d.ts doesn't compile
  • integrating disqus with emberjs only works on first…
  • Postman gives 401 Unauthorized - Spring Boot & MYSQL
  • Trying to integrate Ant Design using Sass and…
  • Spring security CORS Filter
  • Item position in RecyclerView only changing when…
  • Spring Security exclude url patterns in security…
  • What does the "yield" keyword do?
  • How to Update Database from Assets Folder in App
  • How to generate a random number in C++?
  • PHP: How to remove all non printable characters in a string?
  • Encrypting & Decrypting a String in C#
  • Design DFA accepting binary strings divisible by a…
  • Ukkonen's suffix tree algorithm in plain English
  • What is a NullReferenceException, and how do I fix it?
  • Knight's tour Problem - storing the valid moves then…
  • How to load partials / views / templates dynamically…
  • Problems using Maven and SSL behind proxy
  • The theme changer for my code does not seem to work
  • How to handle invalid SSL certificates with Apache…
  • "PKIX path building failed" and "unable to find…
  • How to format a phone number in a textfield
  • collision detection from picturebox in a list c#
  • Trouble with Next js + Express deployment using Zeit Now
  • How to create ranges and synonym constants in C#?
  • Drawing a simple line graph in Java
  • org.hibernate.MappingException: Could not determine…
  • Best way to randomize an array with .NET
  • The remote certificate is invalid according to the…
  • What is the equivalent of node.js .getHeaders() in…
  • PHP random string generator
  • problem with client server unix domain stream…
  • Spring Test & Security: How to mock authentication?
  • "The remote certificate is invalid according to the…
  • Select Tag Helper in ASP.NET Core MVC
  • including regression coefficient and pvalue in the ggplot2
  • How can I hash a password in Java?
  • PKIX path building failed in Java application
  • Java Spring JPA DB. Connections between tables
  • How to get active user's UserDetails
  • Dynamic LINQ OrderBy on IEnumerable / IQueryable
  • lexers vs parsers
  • SQL query return data from multiple tables
  • C# error: "An object reference is required for the…
  • Why does Spring security throw exception…
  • Generate random password string with requirements in…
  • Randomize a List
  • Serializing PHP object to JSON
  • Hash and salt passwords in C#
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Equals(=) vs. LIKE
  • Ember sortBy on property:desc is not the same as reverse
  • Having trouble with my nav bar/header, It used to…
  • Understanding generators in Python
  • How to prevent parent component from reloading when…
  • What are access specifiers? Should I inherit with…
  • Python: print a generator expression?
  • How to properly seed random number generator
  • Undefined reference to 'vtable for ✘✘✘'
  • Palindromic numbers in Java
  • WAVE file unexpected behaviour
  • Tokenize mathematic string expression

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I do a case-insensitive string comparison?

Next Post:

What does the ‘static’ keyword do in a class?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error