random number

This is a discussion on random number within the C++ Programming forums, part of the General Programming Boards category; i looked at the tutorials of generating random numbers and it was too hard to understand .I just want one ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    random number

    i looked at the tutorials of generating random numbers and it was too hard to understand.I just want one random number to be generated at the start of the program between 1 and 100 inclusive could anyone help?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    srand(time(NULL));
    int num = (rand() % 99) + 1;
    Or something along the lines of this.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Here's the main part of that code:

    Code:
    #include <ctime>
    
    int main (void)
    {
        srand  (time(NULL));
    
        int number;
    
        //number = (rand() % (max - min + 1) + min);
        //max = 100, min = 1
    
        //just remember - rand () % some_int will return a value
        //between 0 and (some_int - 1)
    
        number = (rand() % (100 - 1 + 1) + min);
    
        return 0;
    }
    [edit]
    Desolation's code was fine actually
    [/edit]
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    class RandomNumber {
        public:
            RandomNumber() : min(0), max(1) { }
            // other constructors
    
            inline operator int() {
                return ((rand() % (max - min)) + min);
            }
        public:
            int min, max;
    };
    My compiler complains about this code but whatever, it should work.

    Edit: Usage.
    Code:
    RandomNumber randy;
    int rand_num = randy;
    Last edited by Desolation; 05-27-2006 at 11:44 PM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    why should min and max both be zero if we use a constructor with no arguments? I'm sorry it just makes no sense. If you do that than rand() gets devided by zero?! Never even risk deviding by zero!

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    I assumed that is was obvious you would set both min and max... You can set max to 1 to get rid of the problem.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You're abusing OOP here's a function which should do the same thing:

    Guaranteed idiotproof!
    Code:
    int randnum(long min = 0, long max = RAND_MAX)
    {
    	min %= RAND_MAX + 1;
    	max %= RAND_MAX + 1;
    	if(min == max)
    	{
    		return min;
    	}
    	if(min > max)
    	{
    		std::swap(min, max);
    	}
        return rand() % ((max - min + 1)?(max - min + 1):RAND_MAX) + min;
    }
    Usage

    Code:
    randnum(1, 5); //1 ~ 5
    randnum(5, -5); //-5 ~ 0 ~ 5
    randnum(-70, -4); //-70 ~ -4
    randnum(6, 6); //6
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09: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 tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM

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