• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

random numbers

Guest
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

Hi i did have some code that made up a alfanumeric random number

i know it took from a list of abcdefghijklmnopqrstu0123456789

anyway i have lost it and i need a random afla-numeric does anyone know the code to do this?
TOPICS
Advanced techniques

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 31, 2007 May 31, 2007
is this the code you have 'lost'?:
http://www.cflib.org/udf.cfm?ID=529

--

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

Votes

Translate

Translate
LEGEND ,
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

Generate a random number and convert it to hex.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

> Generate a random number and convert it to hex.

That's not going to help with the G-Z part of the alphabet.

To the OP: why are you trying to do this?

To answer your original question:
1) create a list of valid characters, like you have
2) select a random number between 1 and [length of that list]
3) get the character from the list @ that position and store it in your
result string
4) repeat 2-3 until you have a string of the desired length.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

hi Adam, that sound like what i had before, but what is the code to do this?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

quote:

Originally posted by: JohnGree
hi Adam, that sound like what i had before, but what is the code to do this?



The functions randrange and listgetat, combined with the concatonation operator should work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

> hi Adam, that sound like what i had before, but what is the code to do this?

Sorry mate, I rarely provide code for solutions that really oughtn't be too
far from the grasp of the person needing it. I'm a firm believer in the
"teach a person to fish" approach to problem solving. Call me a b@st@rd if
you like.

Read the docs.

Or post your code here, and we can go through where it could use
modification.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 30, 2007 May 30, 2007

Copy link to clipboard

Copied

Hex is a good call, as it will prevent any *UNFORTUNATE* letter combinations

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2007 May 31, 2007

Copy link to clipboard

Copied

The code

<cfset rndm = lcase(replace(CreateUUID(), "-", "", "all"))>
<cfoutput>#rndm#</cfoutput>

will generate a random string of 32 characters from the list a,b,c,...,f,0,1,2,...,9. You can apply a function like left(), right() and so on, to obtain a shorter string.





Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2007 May 31, 2007

Copy link to clipboard

Copied

is this the code you have 'lost'?:
http://www.cflib.org/udf.cfm?ID=529

--

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2009 Jul 20, 2009

Copy link to clipboard

Copied

You can make it super complicated:

number = Math.random()*35

if(number ==10){

     number = a

} else if(number ==11){

     number = b

} else if(number ==12){

     number =c

} else if(number ==13){

     number =d

} else if(number ==14){

     number =e

} else if(number ==15){

     number =f

}

etc.

It would be super long, but it works.( number is the name of a variable )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 20, 2009 Jul 20, 2009

Copy link to clipboard

Copied

This code is also OK ( It seems that you just want a - u, change the code if you like)

        list = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

        character = list[Math.round(Math.random()*35];

This code is a lot more simple!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 20, 2009 Jul 20, 2009

Copy link to clipboard

Copied

LATEST

If you have a list of letters to draw from, and don't mind if letters are repeated, then your algorithm (easily implemented in just a few lines of code) looks something like this:  (don't ask me to write it...)

  1. Initialize the result-variable to an empty string.
  2. Repeat the following for as many characters as you need...
    1. Select a random index in the range 0 <= n < character_list_size by calculating Trunc( Rand() * character_list_size ) where Trunc is a function that truncates to an integer and Rand is a function that returns a random floating-point number 0 <= n < 1.0.
    2. Append {the character in the list at that position} to the result.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation