Solved

Convert int to char

Posted on 2007-11-26
4
245 Views
Last Modified: 2010-04-15
What does this calculation mean?
char ch;
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));

I got it from
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx
0
Comment
Question by:JRockFL
  • 2
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20354352
>>26 * random.NextDouble()
a decimal value between 0 and 26

>>26 * random.NextDouble() + 65
a decimal value between 26 and 91

>>Math.Floor(26 * random.NextDouble() + 65)
just the integral portion of the previous value (still is decimal type)

>>Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))
previous value converted to an integral type

>>Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
previous value converted to a character using the ASCII code

Result: a random character between 'A' and 'Z' (uppercase)

0
 
LVL 8

Author Comment

by:JRockFL
ID: 20354412
Thank you so much for the reply,
A couple questions...
How does 26 * random.NextDouble() equate to 0 and 26?
I was thinking it was being multiplied.
Could you also do random.Next(0,26) if decimal is not needed?

Also, do we even need to work with a decimal type?

 So if we Convert.ToChar( Any number between 26 and 91) we get a random character?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20354481
NextDouble() Returns a random number between 0.0 and 1.0. So you have to multiply to have a bigger range.
And yes, Next(0,26) would be better, but even better Next(26,91).
But for legibility sake, I would use


ch = (char)random.Next((int)'A', (int)'Z');
0
 
LVL 8

Author Comment

by:JRockFL
ID: 20354581
Thank you so much, I appreciate the help and clarity.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now