Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 375 376 [377] 378 379 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887520 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5640 on: March 29, 2014, 04:15:39 pm »

There is in fact a better way:

Code: (Preparation) [Select]
totalElements = 0
for each list in listOfLists
    totalElements += list.length
Code: (Generation) [Select]
i = rand.nextInt(totalElements)
for each list in listOfLists
    if i < list.length
        return list[i]
    else
        i -= list.length
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5641 on: March 30, 2014, 05:01:05 am »

There is in fact a better way:

Code: (Preparation) [Select]
totalElements = 0
for each list in listOfLists
    totalElements += list.length
Code: (Generation) [Select]
i = rand.nextInt(totalElements)
for each list in listOfLists
    if i < list.length
        return list[i]
    else
        i -= list.length
That looks like my suggestion, actually :P
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Sinlessmoon

  • Bay Watcher
  • [Trollinging Intensifies]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5642 on: March 30, 2014, 05:27:49 am »

Ptw because I'm about to seriously delve into programming.

Gotta get back to my precious C#. :P

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5643 on: March 30, 2014, 06:02:48 am »

I've recently been playing around with creating and joining threads in static initialization/deinitialization (C++). That is, pre-main and post-main. Short version: don't do it. Even if it works in one case, it will probably fail randomly somewhere down the line, and it's probably not portable.

Case in point: this short program works fine on gcc, but with msvc it hangs on shutdown. After main ends, the thread stops and is joined, but it hangs on the join() call.

Code: (C++) [Select]
#include <chrono>
#include <thread>

bool isLooping = true;

struct TestClass{
std::thread thread;
~TestClass(){
isLooping = false;
this->thread.join();
}
};

TestClass testClass;

void AsyncLoop(){
while(isLooping){
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

int main(){
testClass.thread = std::thread(&AsyncLoop);

return 0;
}
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5644 on: March 30, 2014, 04:02:58 pm »

I've recently been playing around with creating and joining threads in static initialization/deinitialization (C++). That is, pre-main and post-main. Short version: don't do it. Even if it works in one case, it will probably fail randomly somewhere down the line, and it's probably not portable.

Yeah this was my gut reaction:
Quote
don't do it.

Why would do that anyway? I'm genuinely curious as to what legitimate use it could have.

Pardon my C++ ignorance, but isn't pretty much anything "pre-main/post-main" non-portable?
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #5645 on: March 30, 2014, 04:34:48 pm »

You can do pre-main portably I believe, but you have to have a really good grip on the few guarantees you have over how initialisation happens which you only really get with static variables inside functions. And deinitialisation is much more of a free-for-all.

The only real usage of it I've found that doesn't fall into the category of "don't do it" is for registering test fixtures in a testing library, so that the underlying code adds each fixture to a global collection of fixtures before you hit the main function.
Logged

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5646 on: March 30, 2014, 09:24:24 pm »

Why would do that anyway? I'm genuinely curious as to what legitimate use it could have.

I wanted to create a thread on program startup and have it end on program shutdown, without having to call Init and DeInit functions or something. It would be really nice if there were an easy, clean way; does anyone have any suggestions?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5647 on: March 31, 2014, 04:52:17 am »

Use init and destroy methods. This may look like a disadvantage because you have to put two function calls in your main method, but it has the advantage of being well-defined, and you can run some quick tests before calling init() to see if it's actually worth allocating those resources or if there's something wrong (e.g. user error).
Logged

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5648 on: March 31, 2014, 06:20:06 am »

It was part of a small logging library, but, yes, that's the solution that I went with in the end. Better yet, I wrapped Init and Deinit in the constructor and destructor of a class. RAII, yo.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5649 on: March 31, 2014, 04:44:30 pm »

So I'm trying to use a Python Perlin Noise thing I found a Github. Installations instruction are as follows:
Quote
Installation uses the standard Python distutils regime:

python setup.py install

This will compile and install the noise package into your Python site
packages.
I have absolutely no idea what this means. Halp?
(I use Windows by the way. This would be kinda trivial with Ubuntu)
« Last Edit: March 31, 2014, 04:50:23 pm by Dutchling »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5650 on: March 31, 2014, 04:50:23 pm »

I have absolutely no idea what this means. Halp?

Download the thing, open a shell, navigate to the folder containing setup.py, then run the command "python setup.py install". At least that's what it says there.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5651 on: March 31, 2014, 04:51:05 pm »

So there's no proper way to install it :x?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5652 on: March 31, 2014, 04:52:02 pm »

Well, that's what the instructions say, and it looks like the setup.py actually internally uses the proper way to install it.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5653 on: March 31, 2014, 04:58:19 pm »

I opened command prompt and navigated to my download folder.
Quote
'python' is not recognized as an internal or external command, operable program or batch file.

This is kinda the reason why I always try to avoid github as much as possible >.<
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5654 on: March 31, 2014, 05:08:16 pm »

Where did you install Python and which version is it?

You may have to type something like "drive:\path\to\python.exe setup.py install" e.g. "C:\Python26\python.exe setup.py install"
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!
Pages: 1 ... 375 376 [377] 378 379 ... 796