Thread: How to modify this rand

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    How to modify this rand

    How can I make it so this rand gives out numbers that are increments of 50

    Code:
    int wa = (rand()%700 + 300);
    I already have it so the numbers are between 300-1000 just tried about everything to get it to work but can't seem how too. Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    wa = ( (rand % 15) * 50) + 300;
    Basiclly there are 15 incraments of 50 between 300 and 1000.

    Edit: 15 incraments include 300 and 1000.
    Last edited by Thantos; 08-07-2003 at 08:52 PM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    Thanks... didn't think about it like that... make it work the way you want to right

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The only problem with that, according to this, is that the low order bits aren't very random. You might want to adapt what they have on that site.

    (btw, that link was found here)
    Away.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by blackrat364
    The only problem with that, according to this, is that the low order bits aren't very random. You might want to adapt what they have on that site.

    (btw, that link was found here)
    I haven't seen evidence of this. I would like to see their proof. After all, 80% of the programmers use random numbers Statistics are soooo useful.

    As a test, try this program to see if randomization is not accurate:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i,j;
        for (i=0; i<500; i++)
        {
            j = rand();
            printf(" %04X  %02X  %02X   ", j, j >> 8, j & 0x0FF);
        }
        return 0;
    }
    Display is optimized for multiples of 16 character (or 80 character) width
    Last edited by WaltP; 08-07-2003 at 08:00 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    > I haven't seen evidence of this. I would like to see their proof
    It was only literally true in some very old implementations.
    Standard library rand() generators are typically linear congruential generators (LCG) (like in Visual C++ 6), but they avoid this FAQ issue by returning the high order bits.
    So we should stop propogating the fallacy....

    > Statistics are soooo useful.
    Indeed, as is cryptography.
    The standard library rand() is useless for serious use in both fields.
    This shows how badly a standard LCG-based rand() performs statistically.
    ... and only mention this fact when the process requires absolute randomization. Normal programming should have no problem with the standard randomization.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    66
    If you understand why rand() is insufficiently random, then you are probably using a random number generator that suits your needs... for everything else there is rand().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand() technical question
    By freakme in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 10:22 PM