Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. rand ( )

rand ( )

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means. #include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; } Thanks, Dave :-D

    "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

    B J D 3 Replies Last reply
    0
    • D DaveE9th

      I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means. #include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; } Thanks, Dave :-D

      "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      If you don't use srand(TIME(NULL)) to seed rand(), you'll get the same random numbers every time! if the maximum should be 150 you can do the following:

      int nRandomNumber = (rand() % 150) + 1;

      your code will produce linker errors! int rand() is a declaration. the rand function does not take parameters and your main function does not return a value! hope this helps...

      A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

      D 1 Reply Last reply
      0
      • D DaveE9th

        I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means. #include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; } Thanks, Dave :-D

        "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        srand() seeds the pseudo random number generator. Many, if not most, developers just use the time function for this. rand() returns a pseudo random number from 0 to RAND_MAX. To get a random number in an range, use the modulo operator with the max value plus one. In other words to obtain a random number from 0 through 5 do the following: int x = rand() % 6; Never heard of nrand().

        D 1 Reply Last reply
        0
        • J Joe Woodbury

          srand() seeds the pseudo random number generator. Many, if not most, developers just use the time function for this. rand() returns a pseudo random number from 0 to RAND_MAX. To get a random number in an range, use the modulo operator with the max value plus one. In other words to obtain a random number from 0 through 5 do the following: int x = rand() % 6; Never heard of nrand().

          D Offline
          D Offline
          DaveE9th
          wrote on last edited by
          #4

          Thanks, I'll try those out. Dave :-D

          "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

          1 Reply Last reply
          0
          • B Bob Stanneveld

            If you don't use srand(TIME(NULL)) to seed rand(), you'll get the same random numbers every time! if the maximum should be 150 you can do the following:

            int nRandomNumber = (rand() % 150) + 1;

            your code will produce linker errors! int rand() is a declaration. the rand function does not take parameters and your main function does not return a value! hope this helps...

            A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

            D Offline
            D Offline
            DaveE9th
            wrote on last edited by
            #5

            Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D

            "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

            J B 2 Replies Last reply
            0
            • D DaveE9th

              Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D

              "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #6

              DaveE9th wrote: Does this mean that rand ( ) should only appear in a header file and not in the source file. No it does not and most likely it will be in the source file. John

              1 Reply Last reply
              0
              • D DaveE9th

                I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means. #include <stdlib.h> #include <iostream.h> int main() { int x = 150; int rand (x); return ; } Thanks, Dave :-D

                "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

                D Offline
                D Offline
                DaveE9th
                wrote on last edited by
                #7

                The following two code examples do compile, but only yield a message that says to press a key to continue. Shouldn't I see a random number "x" cout to the screen from rand ( ). What am I doing wrong here? #include <cstdlib #include <iostream> using namespace std; void main() { int x = (rand() % 150) + 1; cout< Here's the other example... `#include <cstdlib> #include <iostream> using namespace std; void main() { int x = rand() % 151; cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_`

                K 1 Reply Last reply
                0
                • D DaveE9th

                  The following two code examples do compile, but only yield a message that says to press a key to continue. Shouldn't I see a random number "x" cout to the screen from rand ( ). What am I doing wrong here? #include <cstdlib #include <iostream> using namespace std; void main() { int x = (rand() % 150) + 1; cout< Here's the other example... `#include <cstdlib> #include <iostream> using namespace std; void main() { int x = rand() % 151; cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_`

                  K Offline
                  K Offline
                  kochhar
                  wrote on last edited by
                  #8

                  They both work fine for me (Win/XP, Vstudio v6). What compiler/system are you using?

                  D 1 Reply Last reply
                  0
                  • K kochhar

                    They both work fine for me (Win/XP, Vstudio v6). What compiler/system are you using?

                    D Offline
                    D Offline
                    DaveE9th
                    wrote on last edited by
                    #9

                    I can't believe what my problem was. I'm still learning VC++ 6.0. I wanted to try some new code so I just replaced the code on my screen with the new code. I went to compile and evertyhing looked ok, it wasn't. I found something weird (at least I think it is weird) about VC++ 6.0, it doesn't compile what's on your screen unless you save it first. The source cpp file listed in my view folder did NOT change with the new code I typed in. VC++ compiled the cpp file listed in the view folder, not the cpp file I had open in front of me. So the code in front of me (that was all messed up) was seemingly compiling ok, in reality it was the cpp file in my folder that was compiling ok instead. I did get rand( ) to work finally. I used time(0) as the argument and it works great. Thanks much, Dave :-D

                    "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

                    1 Reply Last reply
                    0
                    • D DaveE9th

                      Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration? Thanks, Dave :-D

                      "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

                      B Offline
                      B Offline
                      Bob Stanneveld
                      wrote on last edited by
                      #10

                      A declaration looks like this: return type name(arg list);. It's predefined, that means that you only have to use it! Therefore it should only appear in a source file! You should seperate 3 things: 1) declaration => Header File 2) Implementation => Source File 3) Call to a function => Source File (within other functions) A Implementation looks like this: return type name(arg list) { // source code } I assume you know how to call a function... hope this helps

                      A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups