Thread: Random Number issue

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Random Number issue

    Code:
    for(;iINum>0;iINum--)
        {
        cout<<fValisHP<<endl;
        fValisHP+=rand(2.0,4.0);
        cout<<fValisHP<<endl;
        }
    
    
    float rand(float arg1,float arg2)
    {
    float fRandom;
    float fRange=(arg2-arg1);
    
    fRandom=arg1+float(fRange*rand()/(RAND_MAX));
    	float ind;
    	for(ind=2.0;ind<=4.0;ind+=0.1)
        	{
        	if((fRandom>=(ind-0.05))&&(fRandom<(ind+0.05)))
        	{
        	fRandom=ind;
        	break;
        	}
        	}
    return fRandom;
    }
    fValisHP is the final output of the program, and as such is a global
    variable. The 'couts' are there to check that value before and after
    the random call. Even though I use the srand function at the beginning
    of the program (take my word for it), the first value output by rand is
    always the same. If I enter an iINum greater than 1 (but not greater
    than 4) the numbers AFTER the first are 'random', but the first one
    still is not. What can I do to fix this?

    btw, daved, I thought I tried the return on the input reject function,
    but I did what you said and it worked this time. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You know that rand() is the name of a function in <cstdio>/<stdio.h>? If you include that file, you might have an issue.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Er......have no clue what you mean...

    But...

    The fix I came up with was to add one to iINum and the
    conditional parameters for iINum (1<iINum<5). Then, I set
    a dummy variable iZero equal to iINum, and inserted it
    in the for loop. Within that I put an if (iZero = iNum) condition
    (the first (and only the first) time the for loop is entered, it will run). Within that if I called the random number function, then
    reset iZero to 0. That first rand() call was NOT added to fValisHP;
    instead I inserted a continue; at the end of the if to regress to
    the for loop. At this point iZero is no longer equal to iINum, and the loop is iterated 1-4 times, and furthermore rand() is different
    each time the program is run.

    I hope this isn't more complicated a solution than it needed to be.
    I amhoping somebody cancome up with or already knows a better one.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What I mean is, in <cstdio> there is a function called rand() already. If you have a function called rand() yourself (and <cstdio>), the linker will get confused and your program won't compile. (Well, maybe it compiles, but it's bad practice.)

    Another thing: you might not want to use floating point variables in for loops. The imprecision sometimes messes it up.

    So what are you trying to do? Get a random number? If rand() returns the same value for each run of your program, call srand(time(NULL)) at the beginning of your program (and include <ctime>).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    I do call srand with the time at the beginning of the program. I just can't figure out why that first number remains the same until I recompile. But at least I figured out a workaround, though I have the inkling that it is sloppy programming. I didn't think about using rand for my own rand function. It compiled fine and still runs alright though. I use the included rand function within my own 'custom' one, since that generates the random number seeded by ctime. The rounding snipped I included takes care of precision since I only care about one place AFTER the decimal and not at all what comes before it. cout.precision won't work because I don't know how many places I will use before the decimal point.

    I only use that ind as an index for that for loop......it works, and since I don't modify it in any way, within the loop, it works perfectly.

    I have since completed my 'first' C++ program and debugged it too. I'm pretty proud considering I was drawing off of limited knowledge. I know there are better ways to work around some of the code I wrote and problems I encountered (like the first random number). Ah well, I will learn one day.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well post your latest effort, we're not psychic!

    There's no use saying you did "this" and you tried "that" without posting what it actually was that you did, or what your current program looks like.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Issues
    By sansterre in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 05:59 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM