Thread: Adding numbers to a random number

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Adding numbers to a random number

    Hi, i am new to C++. So can anyone help me? I am trying to do some math( either add or multiply) a number to a random generated float( one between 0-0.9999) so it gives an output somewhere between 1-6, i have managed to create the random mumber generated but no matter what i do i cant get the desired output.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Show your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Code:
    
    #include
     
     
     
    <stdio.h>
    
     
    #include
     
     
     
    <stdlib.h>
    
     
    #include
     
     
     
    <time.h>
    
     
    #include
     
     
     
    <conio.h>
    
    
     
    float
     
     
     
     getRand() {
    
     
        
     
     
    return rand() / (RAND_MAX+1.0);
    
     
    }
    
    
     
     
    
     
    int
     
     
     
     main() {
    
     
        srand( time(NULL) );
    
     
        getRand(); 
    
     
    	
     
     
    float number = getRand();
    
     
        printf(
     
     
    "Random number: %f\n", number);
    
     
    	getchar();
    
     
    	
     
     
    int number1;
    
     
        
     
     
    int value;
    
     
    	printf(
     
     
    "enter a number between 1 and 6 \n");
    
     
        scanf(
     
     
    "&i", &number1);
    
     
    	value= number1+number;
    
     
    	printf(
     
     
    "number: %i\f\n", value);
    
     
    	getchar();
    
     
    	getchar();
    
     
    	getchar();
    
     
    }
    



  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You seem to be using C, not C++. It would be helpful if you posted in the right section next time.
    Also remove the excessive whitespace.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Multiply "number1" by "number" instead of adding them, for starters. The output for 1 to 6 will be -6.0f to something slightly smaller than 6.0f
    Devoted my life to programming...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Turn off whatever crappy formatter you have, and just paste plain text.

    This is all the rubbish your "formatter" generated.
    #include
    [/COLOR ][/SIZE ][/FONT ]
    [/COLOR ][/SIZE ][/FONT ]
    [/COLOR ][/SIZE ][/FONT ]
    [FONT=Consolas ][SIZE=2 ][FONT=Consolas ][SIZE=2 ] [/SIZE ][/FONT ][/SIZE ][/FONT ][FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ][FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ][FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ]<stdio.h>[/COLOR ][/SIZE ][/FONT ][/COLOR ][/SIZE ][/FONT ][/COLOR ][/SIZE ][/FONT ]
    [FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ][FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ][FONT=Consolas ][SIZE=2 ][COLOR=#a31515 ]
    [/COLOR ][/SIZE ][/FONT ][/COLOR ][/SIZE ][/FONT ][/COLOR ][/SIZE ][/FONT ][FONT=Consolas ][SIZE=2 ][COLOR=#0000ff ][FONT=Consolas ][SIZE=2 ][COLOR=#0000ff ][FONT=Consolas ][SIZE=2 ][COLOR=#0000ff ]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Adding Random Numbers help
    By mastern200 in forum C++ Programming
    Replies: 6
    Last Post: 01-15-2007, 02:51 PM
  3. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. Replies: 3
    Last Post: 07-24-2002, 08:46 AM