Solved

Chose random value with type of Float.

Posted on 2011-03-23
7
420 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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