generate random number out of the string without string characters in angular

Hi Experts,

Could you please help me in getting the numeric value from the string, currently I am getting characters like asbc.. as well out of the string chars from the given function. Also is there any way to attach current datetime to make it unique if possible.

 public generateRandomString(length: number): string {

    let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    let result = '';

    for (let i = length; i > 0; --i) {

      result += chars[Math.floor(Math.random() * chars.length)];

    }

    return result;

  }
satmishaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
Hi Satmisha,

How about using System.Random(https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.7.1
) - I know it gives you a pseudo-random number but then Math.Random does the same. (https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx)

If you still need to go through the code you have posted, let me know and I will see how can we improve it.

Also I noticed you have tagged the topic with C# and Angular4. Please do clarify if you need a solution in C# or Angular - the sample you have posted is in Angular4.

Regards,
Chinmay.
satmishaAuthor Commented:
Thanks chinmay, appreciate your reply.

I need this solution in angular. As you can see in the logic that I am getting the numeric values out of the string i.e. 12345.. whereas in current implementation I am getting string characters as well i.e. ascd.....

public generateRandomString(length: number): string {

    let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // need numeric values out of this

    let result = '';

    for (let i = length; i > 0; --i) {

      result += chars[Math.floor(Math.random() * chars.length)];

    }

    return result; // expected values is numeric like 12345... and should not contain any alphabet like abcd etc..

  }

Open in new window


I hope it clears things now.
Chinmay PatelChief Technical NinjaCommented:
First thing you should do of you just want the numbers then remove everything else from that 1234...string and just leave the numbers.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Chinmay PatelChief Technical NinjaCommented:
Try this code. I am simply removing the characters that you don't want.

public generateRandomString(length: number): string {

    let chars = '0123456789'; // need numeric values out of this

    let result = '';

    for (let i = length; i > 0; --i) {

      result += chars[Math.floor(Math.random() * chars.length)];

    }

    return result; // expected values is numeric like 12345... and should not contain any alphabet like abcd etc..

  }

Open in new window

Julian HansenCommented:
To answer your question use JavaScript replace()

let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = chars.replace(/\D/g,'');

Open in new window

\D - shorthand RegEx for matching all non-digits
g - match globally
EDIT
To get the numeric value just put a + in front of it or multiply by 1
console.log(+result);
console.log(result*1);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
satmishaAuthor Commented:
Thanks Julian Hansen, it worked.
satmishaAuthor Commented:
Thanks Julian Hansen, it worked.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.