Thread: Random numbers works in one program, not in another

  1. #16
    Shadow12345
    Guest
    (int)((double)rand() / ((double)RAND_MAX + 1) * N)

    I don't even really understand what that means, but I am afraid to ask because I will be told to search for it on google instead of getting an explanation and I will get flamed for all eternity and excommunicated by the persons on the board.

  2. #17
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    I seem to remember Prelude giving an explanation on this. I didn't fully understand it at the time but she usually knows what she is talking about. It was on this board so you may find it by searching.

  3. #18
    Shadow12345
    Guest
    Well I am okay with what i have. By the way is there a way to clear the context of a txt file.

    I thought fout.clear("Generated.txt"); would do it, but it doesnt

  4. #19
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by minesweeper
    I seem to remember Prelude giving an explanation on this. I didn't fully understand it at the time but she usually knows what she is talking about. It was on this board so you may find it by searching.
    What it seems to be saying is that most implementations of rand() dont offer a great amount of randomness for the low order bits and that you are advised to use higher order bits as they are more reliable.....so all the code
    Code:
    (int)((double)rand() / ((double)RAND_MAX + 1) * N)
    is doing is converting the return to a double (adding trailing zeros)...Then once you have limited it to the range you want, you cast it back to an int (this discards the trailing decimal places)...therefore the low order bits of your expression have been discarded and you are left with what should be a better random number sequence......

  5. #20
    Shadow12345
    Guest
    Okay that actually does shed some light on to that...I've never heard of low order or high order bits before though. High order bits are found with doubles because they have more decimal places? and an int has low order bits because it has none?

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I seem to remember Prelude giving an explanation on this.
    Basically, I said that the high order bits of most random number generators tend to be more random than the low order bits, so for more randomized sequences, division by RAND_MAX is better. For more information I'll direct you to Knuth vol.2, page 13.

    Now, for most applications, using the low order bits will work just fine provided the range is wide enough to give a decent dispersion. So there is really nothing wrong with using

    r = rand % N;

    if one is willing to accept less than optimal pseudo-random numbers for that particular implementation. I will warn you now, if any part of this or previous explanations from me confuse you then following up with Knuth will help you little. The Art of Computer Programming series is not for the faint hearted even for looking up and understanding on little tidbit of information.

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

  7. #22
    Shadow12345
    Guest
    Knuth? The Arm of Computer Programming? What is this? A book series? Or is it an ebook or something.

  8. #23
    Shadow12345
    Guest
    So how do I get from SkinColor = rand()%5; to using
    (int)((double)rand() / ((double)RAND_MAX + 1) * N).

    SkinColor = ¿@!****ing g-â-+¼61

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Knuth? The Arm of Computer Programming? What is this? A book series? Or is it an ebook or something.
    The Art of Computer Programming

    >So how do I get from SkinColor = rand()%5; to using
    >(int)((double)rand() / ((double)RAND_MAX + 1) * N).
    SkinColor = (int)((double)rand() / (double)RAND_MAX * 5);

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

  10. #25
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

  11. #26
    Shadow12345
    Guest
    Do you recommend The Art of Computer Programming and The Elegant Universe?

    I recommend Chromosome 6 by Robin Cook, although that has nothing to do with programming.

  12. #27
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do you recommend The Art of Computer Programming
    Yes. I've found that between the three of those books, most programming questions can be answered.

    > and The Elegant Universe?
    Off Topic. Check the GD board for my opinion on this book.

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

  13. #28
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Shadow12345
    Is there some way you can do random numbers between some positive number and another positive number (or negative for that matter)

    for example x = rand() %10 to 20

    Or is that just being silly?
    I don't have time right now to check if this is answered because I'm about to go out the door, but try something like this

    Code:
    x = rand()%(highnum-lownum) + lownum
    I think thats right.

    Edit: Sorry... I had run a search and forgot that it was a search and started reading topics.
    Last edited by Trauts; 09-30-2002 at 04:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Need a little help with Cyclic Numbers Program
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2005, 05:01 PM
  3. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 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. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM