Solved

Convert int to char

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

617 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