Making some random numbers..

This is a discussion on Making some random numbers.. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to generate the same random numbers(i.e. no srand() function needed) but I can't seem to figure out ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    52

    Making some random numbers..

    Hi, I'm trying to generate the same random numbers(i.e. no srand() function needed) but I can't seem to figure out how to get it to work for the range I am in. I want it to be in the range of .5 to 1. Someone hinted that I divide by the constant RAND_MAX and do some other things, but that really seems to do nothing except produce 0. Any help will be nice. sorry for the noob question. And YES, I READ THE FAQ!

    I tried this and it doesn't seem to work when I'd think that it would. It just gives me 0.
    Code:
    ((rand()%50) / 100) + .5
    Last edited by Ripper1; 08-05-2003 at 05:12 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    52
    Oh dumb me for forgetting to realize rand returns an int. Thanks Salem.

  3. #3
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    Random numbers huh? What I usually do is:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctime.h>
    using namespace std;
    
    int main()
    {
      srand(time(0));
      cout << (rand()%19)+1 << endl; // random number between 1 and 20
      cout << (rand()%99)+1 << endl; // random number between 1 and 100
      cout << (rand()%10)+10 << endl; // random number between 10 and 20
      return 0;
    }
    Hope this helps

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>cout << (rand()%19)+1 << endl; // random number between 1 and 20<<
    This is incorrect.
    rand()%19 will get you a number between 0 and 18, adding 1 will make it 1 to 19 (not 20 as your comment states).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User LogicError's Avatar
    Join Date
    Aug 2003
    Location
    г. Магнитогорск
    Posts
    76
    Oh, (rand()%20)+1 then.. that should do it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 03:01 PM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 07:28 AM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 01:59 PM
  5. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21