Thread: Problem building 64bit rand

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    27

    Problem building 64bit rand

    THIRD EDIT: Feel free to delete this problem. Turns out the object hadn't been allocated... so when I'd access it's vars they weren't allocated either. Arg.




    I've been building a transposition table in C++ so it came up that I needed 64 bit random numbers (don't ask..) so I built this function:

    Code:
    long long int Ttable::randomLong()
    {
      int firstHalf, secondHalf;
      long long int newRand;
    
      firstHalf = rand();
      secondHalf = rand();
    
      //copy the first value into the top 32 bits
      newRand = firstHalf;
      newRand = newRand << 32;
      //XOR in the second half covering up all the new zero bits
      newRand = newRand ^ secondHalf;
    
      return newRand;
    }
    And all of the variables I assign to it such as:

    Code:
    white = randomLong();
    or for testing
    Code:
    cerr << randomLong();
    give me segfaults when I go to use em. I know I'm probably missing something simple. If you can't find anything and know of a good *default* 64 bit random number generator that'd also.. probably be fine.

    While I wait I'll go recheck the code with a standard random function... but that should work fine.

    I haven't gotten any useful info from GDB or valgrind other than helping me find that these vars are the ones segfaulting.

    Thanks
    -E



    EDIT:
    I changed to rand() and it still hasn't worked. I must be doing something incorrect at declaration?
    Code:
    long long int white;
    I'm going to back up the files and try out the "standard" 64 bit int, should be int64_t. I hadn't thought that that could be a problem :S. Will report back if change is successful.

    SECOND EDIT:
    Didn't change anything. I still don't see what it's doing that would cause an allocation problem. These aren't pointers :S.
    Last edited by blurrymadness; 05-31-2011 at 11:44 PM. Reason: Problem Solved

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That code can't cause a segfault.

    I don't know where it is because my "Foo" has malfunctioned, but somewhere you have a problem. Be happy you have new knowledge of it.

    Soma

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This speaks volumes for using an approach that makes sure that an object is fully constructed precisely after the constructor is finished. Otherwise you are going to have to add extra checks to make sure the object isn't in a zombie state in all functions or risk doing what you just did.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    This is a little off topic, but if you have a compiler with C++0x or C++03 TR1, the mt19937_64 class will give you 64 bit random numbers with the mersenne twister algorithm. It is a much better way than hacking 64 bits together with rand().
    Code:
    #include <iostream>
    #include <random>
    
    using namespace std;
    
    int main()
    {
        mt19937_64 gen;
    
        for (int x = 0; x < 10; ++x)
        {
            cout << gen() << endl;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porformance problem building a list
    By Hydrahte in forum C Programming
    Replies: 6
    Last Post: 05-06-2011, 04:22 PM
  2. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  3. building problem, need .h file
    By BadWolf in forum C++ Programming
    Replies: 1
    Last Post: 02-14-2009, 12:12 AM
  4. Having a problem building Zthread
    By indigo0086 in forum Tech Board
    Replies: 1
    Last Post: 04-23-2007, 10:56 AM
  5. Problem building a Release
    By The SharK in forum C++ Programming
    Replies: 21
    Last Post: 06-23-2006, 05:21 PM