wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Resources > - Submit a resource -
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 12-12-2011, 07:09 PM   #1
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default [SNIPPET] PseudoRandom_Index

Collapse JASS:
library PseudoRandom

globals
    private integer seed = 1
endglobals

//=====================================================================
//  Return pseudorandom integer that can be used as struct/array index
//  Has full number period (8190).
//  Do not change constants - you will get non-optimal period
//=====================================================================
public function Index takes nothing returns integer 
    set seed = seed * 5 + 17
    set seed = ModuloInteger(seed, 8192)
    if seed == 0 then 
        set seed = 17
    elseif seed == 8191 then
        set seed = 12
    endif
    return seed
endfunction

endlibrary

Tested the function with array sorting.
All numbers are there except number 22. (talk about weird math)

Not much but it does its job.
Wish we had a section for jass functions like on that site I used so long ago I can't even remember it's name....
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.

Last edited by cohadar : 12-13-2011 at 11:21 AM.
cohadar is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 12-12-2011, 11:15 PM   #2
tamisrah
User
 
tamisrah's Avatar
 
Join Date: Jan 2007
Posts: 168

tamisrah will become famous soon enough (48)tamisrah will become famous soon enough (48)

Default

Would you care to provide a sample use case? I can't think of any reason to go through the whole index range of an array randomly.

Why wouldn't there be 22? 5+17=22 if im not totally mistaken.

Does this generate each index once? What happens after 8192 steps?


You meant this site?

EDIT:
So this actually generates every possible index exactly once... How the hell did you create a function like this?
I would totally prefer a small tutorial on how to create those functions over this snippet. xD

Last edited by tamisrah : 12-12-2011 at 11:20 PM.
tamisrah is offline   Reply With Quote
Old 12-13-2011, 12:15 AM   #3
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

Quote:
Originally Posted by tamisrah
Why wouldn't there be 22? 5+17=22 if im not totally mistaken.
Ah ofc, 22 is the first generated number and it got lost because I sorted array from 1 instead of 0.
I guess all numbers are there.

Quote:
Originally Posted by tamisrah
So this actually generates every possible index exactly once... How the hell did you create a function like this?
I would totally prefer a small tutorial on how to create those functions over this snippet. xD
Galois groups, basic algebra really.
Btw 8192 is the biggest Galois group you can create with 32-bit jass integers without getting negative values due to overflow, a really lucky coincidence. :)

Quote:
Originally Posted by tamisrah
Would you care to provide a sample use case? I can't think of any reason to go through the whole index range of an array randomly.
Hashtables are fastest when keys are random and distributed far from each other.

I was hacking around jasshelper and when I saw function vexorian was using for generating hashtable keys I got a headache so I created a proper one.
Than I thought it might be useful to someone in the map as well so I created a jass version too.
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.
cohadar is offline   Reply With Quote
Old 12-13-2011, 08:05 AM   #4
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,190

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

Unfortunately, array index 8191 can bug out so the effective limit is actually 8190.
__________________
Anitarf is offline   Reply With Quote
Old 12-13-2011, 08:39 AM   #5
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

Wasn't that fixed with one of the latest patches?

If not I just need to add one if statement there.

Remind me what was that bug about? Something about campaigns I think...
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.
cohadar is offline   Reply With Quote
Old 12-13-2011, 10:32 AM   #6
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,190

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

The values stored under that index don't get saved correctly when you save a game or something like that.
__________________
Anitarf is offline   Reply With Quote
Old 12-13-2011, 11:20 AM   #7
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

Updated to skip indexes 0 and 8191 because they can be problematic.
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.
cohadar is offline   Reply With Quote
Old 12-13-2011, 04:00 PM   #8
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,190

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

Okay. Now please also tell me what this is supposed to be useful for.
__________________
Anitarf is offline   Reply With Quote
Old 12-13-2011, 04:53 PM   #9
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

Pseudorandom numbers are generally always useful when you want to have random things, but also have them repeat in the exact same order for the same input values.

Remember this option from WE:
File -> Preferences -> Test Map -> Use Fixed Random Seed.

Great stuff for debugging random events.

In vJass compiler it is used for:
Generating random names for 'secret' initstruct functions.
Generating random prefixes for private members of scopes and libraries.
Generating nicely distributed hashtable keys that can also be used as jass array indexes. (keyword key)
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.

Last edited by cohadar : 12-13-2011 at 04:54 PM.
cohadar is offline   Reply With Quote
Old 12-13-2011, 05:33 PM   #10
Bribe
User
 
Bribe's Avatar
 
Join Date: Mar 2010
Posts: 233

Submissions (1)

Bribe will become famous soon enough (30)Bribe will become famous soon enough (30)

Send a message via AIM to Bribe
Default

1. InitStruct functions can just use "private keyword initStruct"

2. Random prefixes are handled by the vJass preprocessor

3. Structs generate a unique integer, too, I can't imagine why you'd want the keys 18 and 56, for example, instead of just 1 and 2. However you can easily recycle those whereas there would be a bit of issues trying to recycle with this, including a fairly consistent scraping of the max array bounds which brings a lot of trouble for efficiency. Compare that to structs which keep the indices as low as possible.

I'm not saying this doesn't have some use, it's just that the reasons you gave are not good reasons. Just looking at it sparks creativity for me, I just don't know of anything specific, offhand. I will let you know if something comes to mind.

Last edited by Bribe : 12-13-2011 at 05:35 PM.
Bribe is offline   Reply With Quote
Old 12-13-2011, 06:19 PM   #11
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

Quote:
Originally Posted by cohadar
In vJass compiler it is used for:
Generating random names for 'secret' initstruct functions.
Generating random prefixes for private members of scopes and libraries.
Generating nicely distributed hashtable keys that can also be used as jass array indexes. (keyword key)

That is it is used in the compiler version I am doing right now.

Collapse pascal:
//==============================================================================
//  Some things need to be random, yet compiled map should always be the same.
//  So we use fixed random seed and pseudorandom number generator.
//  Number is kept positive because we use it in random names.
//  It is also always between [1..8190] so it can be used as array index as well
//==============================================================================
unit PseudoRandom

interface

function GetPseudoRandom(): integer
function GetPrivatePrefix(): string
function GetHashtableKey(): string

//==============================================================================
//  WARNING: Do NOT change any constants in this file.
//==============================================================================
implementation
uses SysUtils

var
  name_seed : integer = 1
  key_seed: integer = 1
  alternator: boolean = false

//==============================================================================
//  PRIVATE: generates next pseudo random number for a given seed.
//==============================================================================
procedure NextPseudoRandom(var seed:integer)
begin
  seed := seed * 5 + 17
  seed := seed mod 8192
  if seed < 0 then seed := seed + 8192
  if seed = 0 then seed := 17
  if seed = 8191 then seed := 12
end

//==============================================================================
// This function is called once for every line of text jasshelper parses.
// It is used to generate random names.
// You always get same random names for same script input.   (pseudorandom)
//==============================================================================
function GetPseudoRandom(): integer
begin
  NextPseudoRandom(name_seed)
  Result := name_seed
end

//==============================================================================
//  Pseudo random hashtable keys.
//==============================================================================
function GetHashtableKey:string
begin
  NextPseudoRandom(key_seed)
  Result := IntToStr(key_seed)
end

//==============================================================================
//  Returns random prefix for private stuff in scopes and libraries.
//==============================================================================
function GetPrivatePrefix():string
begin
  if (GetPseudoRandom() mod 2 = 0) then Result := '__'
  else Result := '___'
end                            

end.

EDIT:
In case someone wonders why I put this into jasshelper, it is because Vex was using real random numbers,
That means that you could not test-compare two compiled outputwar3map.j files because you always get different results.
This way same input script always produces same output script.
But if you change a single line of code it changes all random stuff. (names, prefixes, keys)
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.

Last edited by cohadar : 12-13-2011 at 06:31 PM.
cohadar is offline   Reply With Quote
Old 12-13-2011, 06:51 PM   #12
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,190

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

Quote:
Originally Posted by cohadar
Pseudorandom numbers are generally always useful...
Okay, now how about an example of how this resource is specifically useful, especially considering we already have a pseudo-random number generator built into the game.
__________________
Anitarf is offline   Reply With Quote
Old 12-13-2011, 07:06 PM   #13
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default

The built in generator produces random integers,
this produces random array indexes (in circular fashion, without repeating indexes until it does all 8190).

If you are asking about specific use case in a map I don't know any because resource did not exist before.

But I can bet you now that it does exist someone will come up with a reason to use it.

Replayable Random events in RPG, save/load codes, who knows, there is a lot of crazy people out there.
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.

Last edited by cohadar : 12-13-2011 at 07:07 PM.
cohadar is offline   Reply With Quote
Old 12-13-2011, 10:18 PM   #14
Fledermaus
default string
 
Fledermaus's Avatar
 
Join Date: May 2006
Posts: 705

Submissions (1)

Fledermaus is a jewel in the rough (194)Fledermaus is a jewel in the rough (194)Fledermaus is a jewel in the rough (194)

Send a message via MSN to Fledermaus
Default

Quote:
Originally Posted by cohadar
Updated to skip indexes 0 and 8191 because they can be problematic.
Why is 0 problematic?
Fledermaus is offline   Reply With Quote
Old 12-13-2011, 11:06 PM   #15
Troll-Brain
User
 
Join Date: Oct 2006
Posts: 1,490

Submissions (1)

Troll-Brain has a spectacular aura about (134)

Default

He is just following the vJass struct standard :

null instance == 0

Ofc there is no problem of using the index 0 of an array variable by itself.

And for the index 8191, it makes a saved game unabled to be loaded (wc3 crashes), but i have not tested if this lame bug was fixed ot not in the newest patches.
__________________
Cool != Useful

Last edited by Troll-Brain : 12-13-2011 at 11:08 PM.
Troll-Brain is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 08:53 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services