Solved

Chose random value with type of Float.

Posted on 2011-03-23
7
418 Views
Last Modified: 2012-05-11
The 1st set of attached code works (thank you EE) because I chose a field of type int.  But the actual field I need (EmployeeId) is of type float and I can't get it to work.  Does random not work with type Float?
I'd really like help to either fix the 2nd code to work with a float
or
Get help on how I retrieve the record using the 1st code and then just display the corresponding float field in the form.
I tried the following but got a "no definition, no extension method"  error on the word Tables.
       DataRow[] returnedRows;
       returnedRows = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
1st set Works but wrong variable... 
 var dbTimeClockPlus = new TimeClockPlusDataContext();
            var SearchInfo = (from c in dbTimeClockPlus.EmployeeLists  where c.Suspend == false select c.RecordId).FirstOrDefault();
            Random r = new Random();
            int RcdId = SearchInfo == 0 ? 1 : SearchInfo;
            RcdId = r.Next(1, RcdId);
            txtRcdId.Text = Convert.ToString(RcdId);

2nd set - Correct variable but get several errors regarding conversion from double to int.
var dbTimeClockPlus = new TimeClockPlusDataContext();
            double EmpNo = (from c in dbTimeClockPlus.EmployeeLists where c.Suspend == false select c.EmployeeId).FirstOrDefault();
            Random r = new Random();
            double EmpId = EmpNo == 0 ? 1 : EmpNo;
            EmpId = r.Next(1, EmpId);
            txtEmpNo.Text = EmpId;

Open in new window

0
Comment
Question by:BobRosas
7 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 63 total points
ID: 35200320
Hi, use this code

static float NextFloat(Random random)
{
    double mantissa = (random.NextDouble() * 2.0) - 1.0;
    double exponent = Math.Pow(2.0, random.Next(-126, 128));
    return (float)(mantissa * exponent);
}
 or

static float NextFloat(Random random)
{
    var buffer = new byte[4];
    random.NextBytes(buffer);
    return BitConverter.ToSingle(buffer,0);
}

static float NextFloat(Random random)
{
    var result = (random.NextDouble()
                  * (Single.MaxValue - (double)Single.MinValue))
                  + Single.MinValue;
    return (float)result;
}
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35200470
A little bit off topic - does having the unique ID (I assume it is that based on the name) as a float in the database make sense?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35200796
Exactly.  There are two basic problems with your logic:
1. Random <> unique
2. float is not exact so that x may not equal x

Both of those can be a real show stopper.
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

Author Comment

by:BobRosas
ID: 35200806
samirbhogayta - Thank you for your quick response and 3 choices.  That's great and I really appreciate it!  But the code I currently have I got with the help of EE so I'm not up to speed and I don't know how to use your code and incorporate into my existing code.  I tried using your 1st example (see attached code) but the errors I get are...
   2nd to last line: The best overloaded method match for 'System.Random.Next(int, int)' has some invalid arguments - on exponent line
   2nd to last line: cannot convert from 'double' to 'int'
   Last line: Cannot implicitly convert type 'float' to 'string'
I can really use more help.

AndyAinscow - I think that is an excellent question.  I was wondering that myself.  I'm glad I'm not the only one who finds that odd.  Since I can't change it  I just need to figure out how to work around it.
var dbTimeClockPlus = new TimeClockPlusDataContext();
              double EmpNo = (from c in dbTimeClockPlus.EmployeeLists where c.Suspend == false select c.EmployeeId).FirstOrDefault();
              Random r = new Random();
              double EmpId = EmpNo == 0 ? 1 : EmpNo;
              EmpId = (r.NextDouble() * 2.0) - 1.0;
              double exponent = Math.Pow(2.0, r.Next(1, EmpId));
              txtEmpNo.Text = (float)(EmpId * exponent);

Open in new window

0
 

Author Comment

by:BobRosas
ID: 35200951
acperkins:
Thank you for your input.  I understand what you are saying.  I'd settle for a way to pull up the record based on the end result of the 1st code that does work.  (Each RcdId (int) has only one related EmpId (Float).  But I'm really new to C# and I don't know how to use the RcdId results returned to retrieve the record and just display the EmpId.  
Or can I add EmpId to my select?  Then have the code display the EmpId that corresponds to the returned RcdId.
Or maybe you can suggest an idea?  I really need some help.
Thanks again!
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 62 total points
ID: 35203478
>>Or maybe you can suggest an idea?<<
Well since x does not really equal x there is a strong possibility that you cannot do that in some cases.  If this is not clear, read up on the float data type.  The key point you need to understand is that it is an approximate data type.
0
 

Author Closing Comment

by:BobRosas
ID: 35208000
Once I understand float type better I'm sure samirbhogayta code would help.  For now I think it's safer to just try and select 1 record based on RecordId and then display the EmpId that corresponds to that.  So I'll close this and post another queston.  Thank you for the code and insight.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

22 Experts available now in Live!

Get 1:1 Help Now