Thread: generate a random number

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    generate a random number

    hi all,

    i need to know how to generate a number..influenced by percentages..

    how do u write code ...so for example, there is a 95% chance that a variable = 1 and the other 5% of the time it equals 0...

    right now i got

    #include <cstdio>
    int random = rand () % 2;

    but this is randomly giving me 1 or 0...

    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well for a percentage, you need rand() % 100

    So to get to where you want to be
    int random = rand() % 100 > 95 ? 1 : 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Why do more people use rand than
    Code:
    random(int);
    .

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because rand() is a standard function, and random() isn't
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    6

    random number with probabilities

    Hi, just a follow up question to this thread. So how would you write the code if you have 3 numbers, for example, 60% probability of generating 0, 35% generating 1 and 5% generating 2. Thanks a lot

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    int random = rand()%100;
    int result;
    
    if(random < 60)
    
         result = 0;
    
    else if(random >= 60 && < 95)
    
         result = 1;
    
    else
    
         result = 2;
    Last edited by The Brain; 06-05-2005 at 07:46 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I generate a random number? (easily?)
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 05:04 AM
  2. Generate Random Number
    By peacealida in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2008, 08:57 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM