rand() gives you random numbers right (c#) - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

rand() gives you random numbers right (c#)

zudozudo Posts: 4Member
rand() gives you random numbers right? So why does it always give me 41?!!?!

Comments

  • ssak32ssak32 Posts: 8Member
    : rand() gives you random numbers right? So why does it always give me 41?!!?!
    :

    Actually there r 2 different functions in C which gives u randum numbers when called. Namely:
    1) rand();
    2) randum(int);
    In the first function (i.e., rand();), if u want to get randum numbers with in the range 0-5, then u will have to write the rand(); as follows:

    eg.
    void main(void)
    {
    int rand=0;
    rand = rand() % 5;
    printf("The number generated randumly is %d",rand);
    }

    Now in the second function (i.e., randum(int);), u will have to give the limit of the range u want. i.e., if u want to get a randum number with in the range 0-5 then u will have to pass the number 5 as the parameter to the function.

    eg.
    void main(void)
    {
    int rand=0;
    rand = randum(5);
    printf("The number generated randumly is %d",rand);
    }

    Try this out may be u'r problem will get solved. All the best.



  • ssak32ssak32 Posts: 8Member
    : : rand() gives you random numbers right? So why does it always give me 41?!!?!
    : :
    :
    : Actually there r 2 different functions in C which gives u randum numbers when called. Namely:
    : 1) rand();
    : 2) randum(int);
    : In the first function (i.e., rand();), if u want to get randum numbers with in the range 0-5, then u will have to write the rand(); as follows:
    :
    : eg.
    : void main(void)
    : {
    : int rand=0;
    : rand = rand() % 5;
    : printf("The number generated randumly is %d",rand);
    : }
    :
    : Now in the second function (i.e., randum(int);), u will have to give the limit of the range u want. i.e., if u want to get a randum number with in the range 0-5 then u will have to pass the number 5 as the parameter to the function.
    :
    : eg.
    : void main(void)
    : {
    : int rand=0;
    : rand = randum(5);
    : printf("The number generated randumly is %d",rand);
    : }
    :
    : Try this out may be u'r problem will get solved. All the best.
    :
    :
    :
    :



  • zudozudo Posts: 4Member
    Then it must be my code, I always get 41...
    [code]
    #include "stdio.h"
    #include "stdlib.h"
    main(){


    //Tell them what it is and what to do
    printf("Guess the magic number! Enter a number between 1 and 1000.
    ");
    //Make variables, one if they win, another for the number, and one more for there guess
    int win;
    win = 0;
    int ran;
    int guess;
    //Choose a number
    ran = rand() % 1000;
    //While they haven't won...
    while (win == 0){
    scanf("%d", &guess);
    if (guess < ran) printf("The magic number is bigger, guess again.

    ");
    if (guess > ran) printf("The magic number is smaller, guess again.

    ");
    if (guess == ran){
    printf("You got it right! Huzzah!a

    ");
    }

    }

    }

    [/code]
  • LundinLundin Posts: 3,711Member
    : Then it must be my code, I always get 41...
    : [code]
    : #include "stdio.h"
    : #include "stdlib.h"
    : main(){
    :
    :
    : //Tell them what it is and what to do
    : printf("Guess the magic number! Enter a number between 1 and 1000.
    ");
    : //Make variables, one if they win, another for the number, and one more for there guess
    : int win;
    : win = 0;
    : int ran;
    : int guess;
    : //Choose a number
    : ran = rand() % 1000;
    : //While they haven't won...
    : while (win == 0){
    : scanf("%d", &guess);
    : if (guess < ran) printf("The magic number is bigger, guess again.

    ");
    : if (guess > ran) printf("The magic number is smaller, guess again.

    ");
    : if (guess == ran){
    : printf("You got it right! Huzzah!a

    ");
    : }
    :
    : }
    :
    : }
    :
    : [/code]
    :



    Use srand().

    [code]
    #include
    #include
    #include

    int main(void)
    {
    int i;
    time_t t;

    srand((unsigned) time(&t));
    printf("Ten random numbers from 0 to 99

    ");
    for(i=0; i<10; i++)
    printf("%d
    ", rand() % 100);
    return 0;
    }
    [/code]
Sign In or Register to comment.