Thread: Generating a random number in a specified range and storing to a variable...

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Generating a random number in a specified range and storing to a variable...

    Alright, here's another one for you guys thats got me absolutely stumped.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    #include <stdlib.h>
    
    
    int random;
    
    int main ()
    {
    
    rand(random) &#37; 100;
    cout<<random<<endl;
    system("PAUSE");
    }
    It's output is zero. What am I doing wrong?
    .

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're doing a bunch of things wrong. Read this:

    http://www.cplusplus.com/reference/c...dlib/rand.html

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And this: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    And then, if you're feeling adventurous, this: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

    Other notes:
    • Don't use iostream.h -- it's nonstandard and antiquated. Use <iostream> and a using namespace std; statement instead. (The using statement itself is not the best idea, but one step at a time . . . .)
    • system("PAUSE"), though it works on Windows, is unportable, rather insecure, and quite slow. Consider some alternatives: cpwiki.sf.net/Pause_Console
    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.

Popular pages Recent additions subscribe to a feed