Chose random value with type of Float.

Posted on 2011-03-23
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
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

Question by:BobRosas
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
LVL 11

Accepted Solution

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);

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

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

Expert Comment

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?
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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


Author Comment

ID: 35200951
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!
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.

Author Closing Comment

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.

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Dropdown list with sub group 10 42
aspx ascx, c# 7 40
Returning multiple values with a stored procedure 9 31
SQL State HYT00. Timeout expired proplem 8 42
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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