# Question: Way to obtain a "random" variable from a position

• (2 Pages)
• 1
• 2

## 15 Replies - 668 Views - Last Post: 09 May 2013 - 02:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320672&amp;s=878a6ebcf30ef14c36c063934b4c9cd2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

# Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 12:18 PM

Im making a way of mining with a machine in my game, so I need to make a way of getting the material (ore) and the richness the place where the player placed the machine haves, I have an x and a y position, and I need to get a number "random" but that is allways the same for the position.

Also, I can implement a seed sistem to my game, so the seed can affect the resource.

I have tried by multipling a seed with the position, and rounding it, and I have made a table in a paper to help me thinking, but that didnt work.

Is This A Good Question/Topic? 0

## Replies To: Question: Way to obtain a "random" variable from a position

### #2 anonymous26

• D.I.C Lover

Reputation: 1
• Posts: 3,638
• Joined: 26-November 10

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 12:56 PM

Seed system time then generate your random number from that.

### #3 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:12 PM

ButchDean, on 09 May 2013 - 12:56 PM, said:

Seed system time then generate your random number from that.

But how to generate the richness (a number from 0 to 5) or the resource from the seed and the position?
maybe with the sin or the cos? but the problem with those is that the resource and the richness would be the same for each 360 tiles...

For example:
positions: x=1896, y=230
seed: 0.27391303889453

### #4 modi123_1

• Suitor #2

Reputation: 11494
• Posts: 45,367
• Joined: 12-June 08

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:18 PM

What - are you trying to do something like minecraft? The algorithm that extrapolates that seed value into what ever you are trying to do - that's all on your shoulders.

http://www.minecraft...l_generation%29
http://minecraft.ign...ins-World-Seeds

### #5 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:24 PM

modi123_1, on 09 May 2013 - 01:18 PM, said:

What - are you trying to do something like minecraft? The algorithm that extrapolates that seed value into what ever you are trying to do - that's all on your shoulders.

http://www.minecraft...l_generation%29
http://minecraft.ign...ins-World-Seeds

I know how to generate the seed, thats easy, but the problem is the algorithm, what commands can I use to make it? D:

### #6 malerv

Reputation: 13
• Posts: 100
• Joined: 01-July 09

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:24 PM

ficolas, on 09 May 2013 - 04:12 PM, said:

ButchDean, on 09 May 2013 - 12:56 PM, said:

Seed system time then generate your random number from that.

But how to generate the richness (a number from 0 to 5) or the resource from the seed and the position?
maybe with the sin or the cos? but the problem with those is that the resource and the richness would be the same for each 360 tiles...

For example:
positions: x=1896, y=230
seed: 0.27391303889453

Hi

Do you need the "richness" to be always the same for each game?
Why won't you compute the richness for each tiles at the start of the game and then store it?
If you can t I would look for a oscillating function (or any function with strong divergence) and do something like

i = x*1000+y // depend on the boundaries of x and y
richness = f(i) modulo richness_max

### #7 modi123_1

• Suitor #2

Reputation: 11494
• Posts: 45,367
• Joined: 12-June 08

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:25 PM

The algorithm is up to you.. I have zero idea what you game is, what you need to generate, or any of the rules that _YOU_ the developer should have written down.

### #8 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:26 PM

malerv, on 09 May 2013 - 01:24 PM, said:

ficolas, on 09 May 2013 - 04:12 PM, said:

ButchDean, on 09 May 2013 - 12:56 PM, said:

Seed system time then generate your random number from that.

But how to generate the richness (a number from 0 to 5) or the resource from the seed and the position?
maybe with the sin or the cos? but the problem with those is that the resource and the richness would be the same for each 360 tiles...

For example:
positions: x=1896, y=230
seed: 0.27391303889453

Hi

Do you need the "richness" to be always the same for each game?
Why won't you compute the richness for each tiles at the start of the game and then store it?
If you can t I would look for a oscillating function (or any function with strong divergence) and do something like

i = x*1000+y // depend on the boundaries of x and y
richness = f(i) modulo richness_max

The problem, is that I dont want it to be a patern, and that makes it a patern that repeats each X tiles.

### #9 modi123_1

• Suitor #2

Reputation: 11494
• Posts: 45,367
• Joined: 12-June 08

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:27 PM

Procedural Content Generation:
http://pcg.wikidot.com/

### #10 malerv

Reputation: 13
• Posts: 100
• Joined: 01-July 09

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:43 PM

But if you don't want pattern why don t you generate the richness like this :

for any tiles
tilesRichness = random() modulo richness_max

But as said modi123_1 we can t guess your needs, you should think about the rules your generation should follow.

### #11 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:48 PM

malerv, on 09 May 2013 - 01:43 PM, said:

But if you don't want pattern why don t you generate the richness like this :

for any tiles
tilesRichness = random() modulo richness_max

But as said modi123_1 we can t guess your needs, you should think about the rules your generation should follow.

I dont want any randomness, because to prevent lag, I dont want to save the richness and the resource and check if the machine is there, I want to remove the data when the machine is removed, and when it is placed again, in the same spot, store it in a variable, inside a table, so a loop can go throught the table checking all machines. : D: :

Im looking at the link modi posted, and everything is so complex in my opinion

### #12 modi123_1

• Suitor #2

Reputation: 11494
• Posts: 45,367
• Joined: 12-June 08

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:50 PM

Quote

I dont want any randomness, because to prevent lag, I dont want to save the richness and the resource and check if the machine is there, I want to remove the data when the machine is removed, and when it is placed again, in the same spot, store it in a variable, inside a table, so a loop can go throught the table checking all machines. : D: :

I am not sure if it's a translation issue, but that made no sense.

Quote

Im looking at the link modi posted, and everything is so complex in my opinion

Well yeah.. it's a complex topic. Not everything has some sort of 'boiled down for the masses' write up.. somethings are just inherently complex and advanced.

### #13 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 01:57 PM

modi123_1, on 09 May 2013 - 01:50 PM, said:

Quote

I dont want any randomness, because to prevent lag, I dont want to save the richness and the resource and check if the machine is there, I want to remove the data when the machine is removed, and when it is placed again, in the same spot, store it in a variable, inside a table, so a loop can go throught the table checking all machines. : D: :

I am not sure if it's a translation issue, but that made no sense.

Quote

Im looking at the link modi posted, and everything is so complex in my opinion

Well yeah.. it's a complex topic. Not everything has some sort of 'boiled down for the masses' write up.. somethings are just inherently complex and advanced.

Sorry my mother tongue isnt english :S

What I want the code to do is this:

-The machine is placed, then, the code generates the richness and the resource from the position and stores it in a array.
-When the machine is destroyed, the array is removed, so the code doesnt need to check every tick to see if there is a machine in that spot.
-When the machine is placed in the same spot, the same richness and the same resource is generated, because it has been generated with an algorithm.

What I have atm, is:

-The code randomly generates the richness and the resource, and stores it in a variable
-Each tick, the code checks all the variables inside the a table, to see if there is a machine placed in each spot a machine has been placed in.
-When the player removes the machine, nothing changes, so each tick the code checks for all machines that have been placed, even if they have been removed.

### #14 modi123_1

• Suitor #2

Reputation: 11494
• Posts: 45,367
• Joined: 12-June 08

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 02:07 PM

What does "machine" mean? Is this some sort of in game object, or an actual pc on a network?

Quote

-The machine is placed, then, the code generates the richness and the resource from the position and stores it in a array.

Yeah.. that math is something you need to do. Read up on the procedurally generated content.. Define the variables, variances, etc.. and then sketch out some math algorithms.. sequences... etc. This is, literally, all up to you. There is no magic formula since each game is different.

### #15 ficolas

Reputation: 0
• Posts: 10
• Joined: 09-May 13

## Re: Question: Way to obtain a "random" variable from a position

Posted 09 May 2013 - 02:10 PM

modi123_1, on 09 May 2013 - 02:07 PM, said:

What does "machine" mean? Is this some sort of in game object, or an actual pc on a network?

The machine is a object in the game, a mining drill.
I have read some stuff in the link you sended, im going to try one of them.