Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert int to char

Posted on 2007-11-26
4
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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