Solved

Chose random value with type of Float.

Posted on 2011-03-23
7
416 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

16 Experts available now in Live!

Get 1:1 Help Now