I didnt notice this about random :(

This is a discussion on I didnt notice this about random :( within the C++ Programming forums, part of the General Programming Boards category; With the rand() function rand keeps going higher with the number ex: 4 then 15 then 27 then 35 then ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    114

    I didnt notice this about random :(

    With the rand() function rand keeps going higher with the number ex: 4 then 15 then 27 then 35 then 41 then 61 then 85 then 99 then 1 < until it reaches the number its max is any way to make that obsolete or is it just the way rand works

    even if u first have max as 200 and min as 1 and it comesx out with 56 if u do 100 and min 1 it will be higher
    Code:
    #include<iostream>
    #include<cmath>
    #include<ctime>
    
    using namespace std;
    
    int main()
    {
        int random;
        int max;
        int min;
        while(random)
        {
                     cout << "Enter a max: ";
                     cin >> max;
                     cout << "Enter a min: ";
                     cin >> min;
        srand(time(NULL));             
        random =(rand() % max) + min;
        cout <<"Hey the numbereno is " << random << "\n";
        }
    }
    also im still looking for a sure way that max will always be the highst number reachable and min will always be the lowest cause for some reason if 2000 is max and min then the answer is liek 3759
    Last edited by Nathan the noob; 02-21-2009 at 05:41 PM.
    Who needs a signature?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, if you use % max, you will get a number from 0 to (max-1). If you add min to that, you will get a number from min to (max-1)+min. If you want to go from min to max, then you need to change what you % by, so that at the end (n-1)+min = max. You should be able to solve that for n.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You're only supposed to call srand() once, not every time you go through the loop.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Only use srand(time(NULL)); one time in your program, generally when it starts (in your program, I would put it between int min; and while(random)

    srand() is meant to seed rand()'s algorithm once and then the sequence you get from rand() will be random. When you call srand() again, you start the sequence over, which defeats the way rand() works.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    It never ceases to amaze me, how many times per week people make this same mistake. Does nobody ever RTFM any more?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

    iMalc wat mistake?

    And nvm il just atempt to rite my own random number class
    Who needs a signature?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Nathan the noob View Post
    Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

    iMalc wat mistake?

    And nvm il just atempt to rite my own random number class
    rand()%max

    will give you values from 0 to max-1

    rand()%max+min

    will give values from min to min+max-1
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Writing your own pseudo-random number generator is not all that simple unless you are a big time math guru. There are several open-source algos on the net and there are many books on Amazon for this very thing.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by Nathan the noob View Post
    Im aware about the srand thing this is old coding from like 2 months ago was using to test :P.

    iMalc wat mistake?

    And nvm il just atempt to rite my own random number class
    The mistake is what cpjust pointed out. You seem to think this was just someone's coding style advice or something like that, but what you've been told about is the actual cause of the oddity you are seeing in the numbers generated. You fix it by moving the srand to before the loop.
    I guess in your defence, many articles about rand don't stress enough that you're not supposed to call srand more than once, unless you want to repeat a previous sequence.

    You wont be any better off trying to write your own pseudo random number generator.
    Last edited by iMalc; 02-22-2009 at 11:15 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by iMalc View Post
    It never ceases to amaze me, how many times per week people make this same mistake. Does nobody ever RTFM any more?
    It never ceases to amaze me that modern C libraries don't automatically seed the RNG from time() in the first place, so that people wouldn't have to understand srand().

    If you really want repeatability, you can always call it yourself.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by brewbuck View Post
    It never ceases to amaze me that modern C libraries don't automatically seed the RNG from time() in the first place, so that people wouldn't have to understand srand().

    If you really want repeatability, you can always call it yourself.
    I also think that would have been a good way for it to have been done.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It violates the zero cost principle
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by CornedBee View Post
    It violates the zero cost principle
    Not necessarily... You could have this:

    Code:
    int rand()
    {
        static int initted = 0;
        if(!initted)
        {
            srand(time(NULL));
            initted = 1;
        }
        ...
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The idea would work, but your implementation doesn't. srand() must have access to that initted variable and set it on call, or ... well, you can easily guess what happens when I call srand() with my own seed before ever calling rand().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by CornedBee View Post
    The idea would work, but your implementation doesn't. srand() must have access to that initted variable and set it on call, or ... well, you can easily guess what happens when I call srand() with my own seed before ever calling rand().
    Yeah -- good catch.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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