Thread: sharing what i know about generating random noumbers.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85

    sharing what i know about generating random noumbers.

    tested only with Turbo C/C++

    Code:
     
    //libraries 
    #include <stdio.h> //defines scanf() and printf() 
    #include <stdlib.h> //defines rand() 
    #include <time.h> //defines time() and is needed for randomize() 
    #include <conio.h> //defines clrscr() and getch() 
    
    //main 
    int main() 
    { 
    //declaring a variable 
    int random; 
    
    //clearscreen 
    clrscr(); 
    
    randomize(); 
    random = random(100); 
    
    //display result 
    printf("\nRandom numbers in the range 0-99: %d",random); 
    
    //wait for a key to be pressed by user before program exits 
    getch(); 
    
    }//end main

    Note:

    1) The randomize() initialises the random number generator, rand(), with a value.

    2) The code can work without the randomize() function, BUT without it your computer will only generate same number each time.
    So, the randomize() is there to generate different random numbers with "time" and hence u need to include the library file <time.h>

    3) The function rand(100) generates random numbers between 0 and (100-1). That is numbers between 0-99.

    4) If you want to display 20random numbers, just call the generator in a for loop with "counter <= 20", like for(int counter=0;counter <= 20; counter++)


    well, that's what i know...any suggestions are welcomed!
    Last edited by wakish; 09-25-2005 at 06:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 02:49 PM
  3. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  4. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  5. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM