Thread: random number between two values

  1. #1
    Registered User Solikawa's Avatar
    Join Date
    Mar 2003
    Posts
    2

    random number between two values

    Hi I wrote a program for a programming class in which we had to make a test in which to see what an earthquake is on a richter scale. Here it is:
    #include "stdafx.h"
    #include "stdio.h"
    int main(int argc, char* argv[])
    {
    int n = 0;

    if(n <= 5.0)
    {
    printf("There is little or no damage dealt by the earthquake!");
    }
    else
    {
    if(n >= 5.0 && n < 5.5)
    {
    printf("Some damage was dealt by the earthquake!");
    }
    else
    {
    if(n >= 5.5 && n < 6.5)
    {
    printf("Serious damage was dealt as a result of the earthquake!");
    }
    else
    {
    if(n >= 6.5 && n < 7.5)
    {
    printf("Major disator from the earthquake!");
    }
    else
    {
    if(n>7.5)
    {
    printf("There is a state of emergency because the earthquake has totaled most buildings!!");
    }
    }
    }
    }
    }

    return 0;
    }


    My question is how do I make a random number between 1 and 8 including both 1 and 8 and assigning it to n so it really shows how the program works and if I do stuff like this it's worth xtra credit. I sorta need it b\c the teacher sucks and doesn't really state clearly when we have assignments. Thanks ^_^

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My question is how do I make a random number between 1 and 8 including both 1 and 8 and assigning it to n
    Well, you could read the FAQ, which already has this very common question answered with full source code. You can find helpful links splinkled all over ths site, or go here.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Solikawa's Avatar
    Join Date
    Mar 2003
    Posts
    2
    hmm.. i didn't think of doing that while i was searching the post for answers. I know how to be on a message board, those other threads didn't answer my question, they beat around the bush without providing real answers without digging them out, so I asked my question

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    n = (rand() % 8) + 1;

    quick example.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Next time you post your code, use code-tags

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > I know how to be on a message board, those other threads didn't answer my question, they beat around the bush without providing real answers without digging them out
    You didn't look very hard then. I've answered this question countless times very directly, every answer to it I've seen that wasn't mine did so as well.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 02:16 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  5. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM