Solved

Convert int to char

Posted on 2007-11-26
4
236 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

10 Experts available now in Live!

Get 1:1 Help Now