Thread: problems with generating random numbers

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    problems with generating random numbers

    Hi everybody,
    I have a problem with a fuction that should generate a random number between 0 and 1 but it only generates 0 everytime. i dont know what's wrong anymore, if someone could help me i would be greatful. Thanks. Here is the code:
    float generare(void)
    {
    int r;
    float U;
    randomize();
    r=random(100);
    U=r/99;
    return U;

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Where do the functoins random() and randomize() come from?

    In C you can include the time.h and stdlib.h header files and seed like this
    Code:
    srand( time( NULL ) );
    and generate random numbers like this
    Code:
    randNum = rand() % 100 + 1

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: problems with generating random numbers

    >>problem with a fuction that should generate a random number between 0 and 1 but it only generates 0 everytime.

    http://www.eskimo.com/~scs/C-faq/q13.18.html
    http://www.eskimo.com/~scs/C-faq/q13.16.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I have a problem with a fuction that should generate a random number between 0 and 1
    The solution is really very simple :-)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    static double generate_rand(void)
    {
      return (double)rand() / RAND_MAX;
    }
    
    int main(void)
    {
      int i;
    
      for (i = 0; i < 20; i++)
      {
        printf("%f\n", generate_rand());
      }
    
      return 0;
    }
    You can seed it if you'd like also.
    *Cela*

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    This may work

    I think the problem with your code is that the result of the randomize() should be stored in a float. Try the following code -
    Code:
    float r,u;
    r = randomize(100);
    u = r/99.0;
    printf("%f",u);

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Sorry

    Code:
    r = random (100);

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    Thanks everybody,
    I still see my version as being the easiest, i just haven't found yet the mistake. Probably isn't a big mistake, that's why i cant see it.
    Oh, and thanks Code Jerk but it cant work with float, randomize and random are function that return an integer. Look up into the C library, stdlib. Anyway, any idea is welcomed, i'm still trying to find the mistake.
    Thank you all.

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    You have r as an int, which you then divide by 99 and get, shock horror! 0
    Make r a float, then see what happens.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    9
    here is the code again:
    float generare(void)
    {
    int r;
    float U;
    randomize();
    r=random(100);
    U=r/99;
    return U;
    So, randomize initialize the random generator. Then, r gets an integer number between 0 and 99. But i need a random number between 0 and 1 so, i make U=r/99, U is float as the result. Now, if i erase the U=r/99 line, things are ok, meaning the function works and i get a random number between 0 and 99. But i need more than that and sounds like the problem is with that U float. I've had problems with floats in C and Pascal too some other times but in the end i made things work. this time i dont get it and also i'm pretty tired.
    Again, thanks alot all of you there.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Change this:
    >U=r/99;

    ... to this ....

    >U=r/99.0;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Thank you all!!!

    Hey there,
    Sorry it toke me so long to reply but i was caught into some exams. First of all i want to thank you all here for your help. Second, Hammer, thank you, that was the problem, a dot and a zero. My project was cool and you helped me when my brain got stucked. Thank you all and hope i'll help you too one day.
    Ioana.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. generating 10 unique random numbers in C
    By creeping death in forum C Programming
    Replies: 4
    Last Post: 01-28-2009, 11:23 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. Random numbers
    By ActionMan in forum C Programming
    Replies: 6
    Last Post: 03-15-2002, 09:26 AM