Thread: rand()

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    21

    rand()

    Yes I know to use srand()
    But does anyone have anything more random?
    Thnx in advance
    -Srg Pepper

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    21
    Thanks. I don't have I compiler on this computer so I can't try it out. Would this work:
    Code:
    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main()
    {
      srand(time(NULL));
      cout<<"A random number: "<<(int)((double)rand()/((double)RAND_MAX+1)*100)<<endl;
      cin.get();
      return 0;
    }
    That should give an integer between 1 and 100, right?
    -Srg Pepper

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I tried it, it's giving me 79 every time. 80 Now, so it's not random, it's 100% based on the time in which it's seeded. I'll check in a minute to see what's wrong.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    21
    Thanks. I would do it myself, but as I said before I don't have a compiler on this comp and I can't download one
    -Srg Pepper

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This is working for me..

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
    	srand(GetTickCount());	
    	cout<<"A random number: "
    	  << rand() / (RAND_MAX / 100 + 1)
    	  << endl;	
    	cin.get();	
    	return 0;
    }
    Although i'm sure you don't want the windows header in there... I can't figure out what's wrong with the regular time() implementation.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    21
    I would prefer not to have the windows header in my program. I'm trying to make this thing non platform specific. Anybody write their own random function?
    -Srg Pepper

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I dont know what the problem with your system is but the ocde you posted works just fine on VC60.

    maybe you had the srand(time(NULL)) in the loop so you kept re-seeding rand with the same value; make sure you only seed once
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    21
    Yes, that would be a problem if you seeded everytime you were going to use rand();
    -Srg Pepper

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 () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM