Random Floating Point Numbers

This is a discussion on Random Floating Point Numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, I am using Code: srand(time(0)); rand() % 100; to create random values. This makes values like 18, 26 etc. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    55

    Random Floating Point Numbers

    Hi,

    I am using
    Code:
    srand(time(0));
    
    rand() % 100;
    to create random values. This makes values like 18, 26 etc.
    I need it to create random floating point values such as 18.6, 26.8 etc.

    How would i get it to create floating point values not integers.

    Thank you.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    '(double)rand() / RAND_MAX' should give you a random floating point number between 0.0 and 1.0. Then you can just multiply it by your upper limit.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Thanks.

    I am using INFp (positive infinity), INFn (negative infinity) and NAN (Not A number) in my program.
    I want to include those in the random floating point values that are created. How would i do that?

    Thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Reserve some numbers or range for them and translate them. Eg:
    if (rnd == 10) rnd = inf;
    etc.

    Also, use nullptr, not 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Khadafi View Post
    Thanks.

    I am using INFp (positive infinity), INFn (negative infinity) and NAN (Not A number) in my program.
    I want to include those in the random floating point values that are created. How would i do that?

    Thanks
    My thoughs are that this is technically impossible. Given infinite set of numbers (so we can include +/-INF), chance of getting any number (including INFs) within this set is 0.
    Reserving a fixed amount of integers for infinities gives chance near 0, because RAND_MAX can be any integer in <32767, INF).
    I would set up own constant X, not greater than RAND_MAX and not greater than N. This would give a finite subset for X (the rand() divisor)
    Or the second solution, reserve X% of upper numbers (of RAND_MAX) for special values. Remember to divide by 100-X% of RAND_MAX to get numbers along with 1.0.
    Why do you want to generate 'not a number' from 'a set of numbers'? This does not make sense to me.
    Last edited by kmdv; 02-26-2011 at 10:04 AM.
    I never put signature, but I decided to make an exception.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, i am creating a sorting algorithm that sorts INFp, INFn and Nans.

    I need to include those in the array that is sorted. So if i am creating random values i need those to be included aswell.

    Is there anyway i can assign something to every say 5th element in the array. That way i can create a normal random array, and then assign some of the special cases into the array.

    Thanks

  7. #7
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    What's wrong with a normal loop then? Loop through your array with a step of 5.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, yes i thought of that, but i don't know how to do the step of 5.

    Thanks

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Hi, i'm trying something like this.

    Code:
    	for (int i=0; i < numberofelements; i+5)
    	{
    		if (i < numberofelements-3)
    		{
    		Array[i] = INFp;
    		Array[i+1] = INFn;
    		Array[i+2] = NAN;
    	        }
    	}
    Number of elements could be any number that the user enters.
    I want to increment by 5, and then set the fifth element to INFp, the 6th as INFn, and the the as NAN.
    Why is this not working.

    Thanks

  10. #10
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Well... That's almost it. "i+5" in your for loop should be "i += 5" or "i = i + 5".

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Code:
    	for (int i=0; i < numberofelements - 2; i += 5)
    	{
    		Array[i] = INFp;
    		Array[i+1] = INFn;
    		Array[i+2] = NAN;
    	}
    Better.
    I never put signature, but I decided to make an exception.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Thank you works perfectly .

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Quote Originally Posted by itsme86 View Post
    '(double)rand() / RAND_MAX' should give you a random floating point number between 0.0 and 1.0. Then you can just multiply it by your upper limit.
    I tried doing

    (double)rand() / RAND_MAX * 15. Using 15 as my upper limit. But i dont really have a upper limit, what is the highest i can set it to.

    Also the values it creates are many digits, what could i do to make them like 13.2 instead of 13.2321. Or maybe have a mixture of some being like 13.2 and some being 13.356 etc.

    Thanks.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Khadafi View Post
    I tried doing

    (double)rand() / RAND_MAX * 15. Using 15 as my upper limit. But i dont really have a upper limit, what is the highest i can set it to.

    Also the values it creates are many digits, what could i do to make them like 13.2 instead of 13.2321. Or maybe have a mixture of some being like 13.2 and some being 13.356 etc.

    Thanks.
    <0, 1) with 4 digits:
    Code:
    (double)(rand() % 10000) / 10000
    <0, 1000) with 1 digit:
    Code:
    (double)(rand() % 10000) / 10
    I never put signature, but I decided to make an exception.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    55
    Thanks.

    So for where it says 10000 can i put any number to make the range greater?.
    Also i was wondering if it is possible to create minus values randomly.

    Thanks.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 02:22 PM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 02:37 PM
  3. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 08:53 PM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 02:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21