Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 462 463 [464] 465 466 ... 796

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

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6945 on: February 02, 2015, 07:46:14 pm »

So you want a normally distributed random variable? Which language are you using? Because there are usually normal  in the standard library. If you don't find any, you can use this method:

Let u, v be two random floats uniformly selected from the unit interval (you do know how to get regular random floating-point random numbers, right?). Let c be your peak, and s be your standard deviation. Then you can get a normally distributed random value by calculating c+s*sqrt(-2*ln(u))*cos(2*pi*v).
« Last Edit: February 02, 2015, 07:51:55 pm by MagmaMcFry »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6946 on: February 02, 2015, 07:57:05 pm »

If you need it faster, then you can pre-compute a table which converts the random output to normals, and do linear interpolation to go the precise value. Much faster than doing square roots, cos and logs every time.

Precomputing tables of values when the program starts, and using interpolation, is also good for quick and dirty cos, sqrts etc when you know the input will be in some pre-defined range. Much faster than using your languages math library functions every time, and can be made as accurate as you want without sacrificing speed (but sacrificing memory instead).

Even better if you can trick your inputs to be ints. For example, i read that in Wolfenstein and Doom they don't use 360 degree circles or radian-based circles. They define a circle with 256 "degrees". Then any angle can be expressed in exactly 1 unsigned byte, you don't need any bounds-checking when rotating, since if it goes over 255 it wraps around naturally. And they just have arrays of 256 pre-computed sin and cos values directly stuck in the code, so they can use a direct index lookup instead instead of needing computation or maths functions. In any game that you can look around but doesn't need super accuracy, using 256-degree circles has some really nice benefits.
« Last Edit: February 02, 2015, 08:08:53 pm by Reelya »
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6947 on: February 02, 2015, 08:09:09 pm »


Stop Misquoting Donald Knuth
Spoiler: Obligatory xkcd (click to show/hide)
Did you know that Knuth has an error decorating his wall?
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6948 on: February 02, 2015, 10:51:16 pm »

So for a couple of hours I've been trying to create a RNG that would create a bell curve, the peak at 1, that I could also affect the spread of with a parameter.
So far I've tried doing what are essentially a bunch of dice rolls, but it seems no matter how many "dice" I have the result can vary massively. I've also tried pissing around a bit with powers but nothing much has come of that.

I'm sure there must be a better way but for the life of my can't think what it is, and I really can't progress until I've worked this out.
Python has random.normal random.gauss, both of which do that.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6949 on: February 03, 2015, 05:18:03 am »

Graknorke, what language are you using? If it's C++, the (new) C++11 random library has std::normal_distribution to do that kind of thing.

Incidentally, if you're still using rand() in C++ code, STOP. It's one of the worst random generators ever created. Do yourself a favour and learn the new C++ random library.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

hops

  • Bay Watcher
  • Secretary of Antifa
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6950 on: February 03, 2015, 02:47:16 pm »

Could anyone explain to me, in general Pseudocode, how I can go about writing a program that create a list containing arrays which have all the possible combinations of orders for an n number of variables?

EDIT: if my wording was confusing, I meant a program that if I, say, put in variable as the strings 'a' and 'b', then it will generate [a, b] and [b, a], and if I put in 'a', 'b', and 'c', then I will get [a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b] and [c, b, a].
« Last Edit: February 03, 2015, 02:52:19 pm by Objective »
Logged
she/her. (Pronouns vary over time.) The artist formerly known as Objective/Cinder.

One True Polycule with flame99 <3

Avatar by makowka

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6951 on: February 03, 2015, 02:54:01 pm »

Okay, usually I just accept whatever people are doing and help out with it, but this time I can't help it.

why

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6952 on: February 03, 2015, 03:00:50 pm »

http://www.cplusplus.com/reference/algorithm/next_permutation/

I assume this is for homework, but that's what you'd most likely use in real life.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6953 on: February 03, 2015, 03:05:26 pm »

Or, if you're hipster and use Python.

import itertools
array = [whatever you want to perm]
results = itertools.permutations (array)


(Also a lot less intimidating in syntax)
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

hops

  • Bay Watcher
  • Secretary of Antifa
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6954 on: February 03, 2015, 03:08:04 pm »

Okay, usually I just accept whatever people are doing and help out with it, but this time I can't help it.

why
I'm trying to check my answer to an NP-hard problem.
Logged
she/her. (Pronouns vary over time.) The artist formerly known as Objective/Cinder.

One True Polycule with flame99 <3

Avatar by makowka

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6955 on: February 03, 2015, 03:12:28 pm »

(Also a lot less intimidating in syntax)
I wish the c++ ranges library would hurry up so I could stop writing begin and end in every algorithm call and just pass in the container (like every other language)
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

Graknorke

  • Bay Watcher
  • A bomb's a bad choice for close-range combat.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6956 on: February 03, 2015, 03:17:54 pm »

Graknorke, what language are you using?
VB.net
I know, I know. I wouldn't be if school didn't force me to.
Logged
Cultural status:
Depleted          ☐
Enriched          ☑

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6957 on: February 03, 2015, 04:55:04 pm »

Could anyone explain to me, in general Pseudocode, how I can go about writing a program that create a list containing arrays which have all the possible combinations of orders for an n number of variables?

EDIT: if my wording was confusing, I meant a program that if I, say, put in variable as the strings 'a' and 'b', then it will generate [a, b] and [b, a], and if I put in 'a', 'b', and 'c', then I will get [a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b] and [c, b, a].

Code: [Select]
function permlists(elements) {
  if (elements.hasAtMostOneElement()) return [elements];
  [anElement, remainingElements] = pickOneElement(elements);
  result = [];
  for (subpermlist : permlists(remainingElements)) {
    for (insertPosition : everyPossibleInsertPositionOf(subpermlist)) {
      result.append(subpermlist.copy().insertAt(insertPosition, anElement));
    }
  }
  return result;
}
Logged

BlindKitty

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6958 on: February 04, 2015, 03:55:01 am »

Graknorke, what language are you using?
VB.net
I know, I know. I wouldn't be if school didn't force me to.

.NET random generator always seemed pretty good to me (but I haven't tested it thoroughly), and it uses method Sample() to generate a double x such as 0 <= x < 1. Getting two of those and using Magma McFry's suggested transformation should give you what you need.

Also, use only one instance of Random class to generate all your results, don't seed it (in .NET, it should be seeded automatically with system time), and you should be good to go.
Logged
My little roguelike craft-centered game thread. Check it out.

GENERATION 10: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6959 on: February 04, 2015, 07:17:15 am »

If you need it faster, then you can pre-compute a table which converts the random output to normals, and do linear interpolation to go the precise value. Much faster than doing square roots, cos and logs every time.

Precomputing tables of values when the program starts, and using interpolation, is also good for quick and dirty cos, sqrts etc when you know the input will be in some pre-defined range. Much faster than using your languages math library functions every time, and can be made as accurate as you want without sacrificing speed (but sacrificing memory instead).

Even better if you can trick your inputs to be ints. For example, i read that in Wolfenstein and Doom they don't use 360 degree circles or radian-based circles. They define a circle with 256 "degrees". Then any angle can be expressed in exactly 1 unsigned byte, you don't need any bounds-checking when rotating, since if it goes over 255 it wraps around naturally. And they just have arrays of 256 pre-computed sin and cos values directly stuck in the code, so they can use a direct index lookup instead instead of needing computation or maths functions. In any game that you can look around but doesn't need super accuracy, using 256-degree circles has some really nice benefits.
The tricks that game devs came up with to go fast never cease to impress me.
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.
Pages: 1 ... 462 463 [464] 465 466 ... 796