RANDOM NUMBERS

I read the post "Random numbers below" and was wondering how I could get the random numbers say between 1 and 10? Any help would be great! Thanks

Joel


Comments

  • : I read the post "Random numbers below" and was wondering how I could get the random numbers say between 1 and 10? Any help would be great! Thanks

    : Joel


    This will put a random number in r between 1 and 10.


    int r;

    r=rand()%10 + 1;






  • First call rand function:

    x=rand()%10;

    and if you wanna generate different numbers

    everytime you run the program call this one first:

    srand( (unsigned)time( NULL ) );

    you will have to include header time.h and stdlib.h


    Razze


  • Hey, that worked great! Thanks

    I have another question :-)

    How would I make that random number able to be random between 46-72?

    :

    : First call rand function:

    : x=rand()%10;

    : and if you wanna generate different numbers

    : everytime you run the program call this one first:

    : srand( (unsigned)time( NULL ) );

    : you will have to include header time.h and stdlib.h


    : Razze





  • There may be an easier way of doing this, but I have used something similar to this in the past successfully.


    first get a random number equal to the range of values (72 - 46 = 26)


    x = rand()%26;


    now we a have a random number 0 through 25. Now write a function to convert it to the proper format


    int converter(int x) {

    if(x == 0) return 46; else

    ...

    ...

    ...

    if(x == 25) return 72;

    }


    Now use the code:


    number = rand()%26;

    number = converter(number);


    to get a random number in the range you need.


    As I said there may be an easier way, but you can modify this method to make a range of random numbers that are all perfect cubes or any other strange set of random numbers that you want.








Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories