Link to home
Start Free TrialLog in
Avatar of BobRosas
BobRosas

asked on

Chose random value with type of Float.

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

ASKER CERTIFIED SOLUTION
Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
Avatar of BobRosas
BobRosas

ASKER

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

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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.