Results 1 to 10 of 10

Thread: random number

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    152

    Exclamation random number

    how would you generate a random number between 1 and 52? including 1 and 52

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Try
    PHP Code:
    int mynum rand()%1+52
    or

    PHP Code:
    int mynum rand()% 53
    if it doesn't include 1 and 52;
    Baaaaaaaaah

  3. #3
    New Member
    Join Date
    May 2002
    Posts
    13
    Try: (rand() % 52) + 1

  4. #4
    DaoK
    Guest
    why you use "%" and not "*" ?
    In VisualBasic or in Java we never use modulo for random operation ?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    int num = int((float(rand()) / RAND_MAX) * 52.0) + 1;
    % isn't valid for floating-point numbers...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    DaoK
    Guest
    anyone can tell me what rand() return ? I have done some test and i return about 3 to 6 decimal number ? why isn't between 0 and 1 like VB or Java?

    I do not understand why we use MOD (%) ? Anyone can tell me a mathematic answer about that ?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You shouldn't need modulus...

    rand() returns a number between 0 and RAND_MAX. So, to get it between 0 and 1, divide rand() by RAND_MAX. Then just multiply by 52.

    I can't see where % would come into it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's very simple. Most random functions return a flloating point number between 0 and 1. CRT rand() returns an integer between 0 and RAND_MAX (which is defined as 32767, the highest positive integer possible for a 16 bit signed variable). To get a number between 0 and n (exclusive), simply do
    rand() % n;
    which puts all numbers in this area. It might not be completly fair (the lowest numbers have a slightly higher probability), but it is fast.
    A slightly more fair approach is parksie's snippet, but it is considerably slower, having one FDIV, one FMUL, two int-to-float conversions (one on good compilers), one ADD and one float-to-int conversion as opposed to on DIV of the first approach (386 DIV instruction computes division and modulo at the same time).

    So, if you don't need any special accuracy, use %. Else, use the floating thing or wait for quantum computers, which can produce real random numbers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    So, if you don't need any special accuracy, use %. Else, use the floating thing or wait for quantum computers, which can produce real random numbers.
    Or give a number to your users and tell them to multiply it by two and enter it in again. Chances are, none of them will ever give the same number
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Unless you're stuck with people who mindlessly follow any instructions the program gives them.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width