Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Random Number Generator Problems

Author
Message
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Jan 2008 05:57
I'm having a problem placing objects randomly. I thought I understood seeding the random number generator but the results are too similar to be useful. In the example program, I have 10 cubes that I want to place randomly at x and z . I create a little radar so I can get a picture of where the objects are in the world. Every time through the loop, the objects group pretty much the same way. Press enter each time after the radar is displayed and it will delete and recreate the objects. I've searched the forum but the answer is always the same: randomize timer() etc. But it doesn't help. Anybody got any ideas?



Enjoy your day.
Attila
FPSC Reloaded TGC Backer
20
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 30th Jan 2008 11:05
Try something like

randomize timer()+mousex()-mousey()

it is more "random" than just randomize because the mouse position adds to the random-effect.
tha_rami
19
Years of Service
User Offline
Joined: 25th Mar 2006
Location: Netherlands
Posted: 30th Jan 2008 11:08
Does something like randomize rnd(timer()) work? That would make it pretty random.


A mod has been erased by your signature because it was larger than 600x120
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 30th Jan 2008 14:24
it sounds like you need some sort of procedural generation (which I am trying to learn but finding very hard)
I have seen programs that generate entire city plans very realistically, but I have no idea how to do it myself
Maybe a noise function would help (i have no idea about that either)

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 30th Jan 2008 18:17 Edited at: 9th Feb 2008 20:33
I have to agree with WindowsKiller.

Computer generated random numbers tend to be just lists of numbers and the seed (like when using Timer()) simply chooses the point in the list that you start reading numbers.

Fiddling around with trying to make the seed any more random is pointless.

You can prove this by using:



For me, at the time of writing, this prints:

0 1 1 3 3 4 0 5 8 7

and does so every time the program is run. Changing the seed to a different number like 1100 results in a different set of numbers - but still the same every time the program is run.

Switching back to 1000 again results in the same series of numbers we got before, so they can't be being generated 100% 'randomly'.

This has it's uses though. You can use fixed seed numbers for your program to generate the same thing when run on everyones machines - like fixed 'random' matrices on each level.

I'm assuming that in the Windows game Freecell, when you select the game to play by selecting a number, you are simply selecting the random generator seed so you always get the same 'random' cards selected each time.

[Edit] After posting, I just took a look at your source code and your problem is caused by the bit range that Rnd() uses. I've not checked it, but it seems to 'wrap around' - in the same way that if you try to store 256 in a byte you end up with 1.

I've never been that interested to test out the max size for a Rnd() number but I'm pretty sure that it's a lot less than the rnd(200000) you have used - probably 65535.

So, use Rnd() with say 1024 and then scale the result up:



TDK_Man

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Jan 2008 19:28 Edited at: 30th Jan 2008 19:38
@TDK_Man

You nailed it TDK! It turns out the highest value rnd() can return is 32767. My range was too high and therefore always grouped the objects within -100000 and -67233 (the difference between 32767 and 100000).

Thank you.

Thanks everyone for your input!

Enjoy your day.
Attila
FPSC Reloaded TGC Backer
20
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 31st Jan 2008 12:14 Edited at: 31st Jan 2008 12:15
Thanks TDL, I learned something about random numbers here, but using time tends to create every time similar numbers. I use the cursor position to create a different seed each time (or so I hope). The rnd() distributes the numbers nicely but as you said depending on the seed it repeats the values when the same seed is used.

I tried to determine the distribution of the numbers and could not find a difference when using timer, timer+mous etc.



If you try it out you will find the numbers are distributed very nice, but there remains the problem to create a different seed each time the randomizer is started.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 9th Feb 2008 12:13 Edited at: 9th Feb 2008 12:17
@TDK
does that mean RANDOMIZE TIMER() doesn't work?!!
Maybe we should be using RANDOMIZE Val(Right$(Str$(Timer()),4))
aaargh my eyes!!

PS How does the computer generate random numbers?
I am trying to learn procedural generation and it would really help to understand random number generators.

Link102
20
Years of Service
User Offline
Joined: 1st Dec 2004
Location: On your head, weeeeee!
Posted: 9th Feb 2008 19:04 Edited at: 10th Feb 2008 13:02
by using a function with 1 input (a seed)
that mathematical function outputs a completly difrent number everytime 1 is added to the input.

wiki

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 9th Feb 2008 22:52
Quote: "does that mean RANDOMIZE TIMER() doesn't work?"


No - it works fine.

The reason many people don't think it works is because it's used a lot to select random X & Z positions on a landscape and the numbers selected - like the landscape have to be very big.

Unfortunately, DB doesn't error if you use an Rnd range which is too big (>32767 as Latch has confirmed). Instead it rounds the number down and carries on - ending up with a small random number range (relative to the landscape).

I don't know the precise technicalities of DB's random number generation, but no computer generated numbers are truly random. They are 'pseudo-random' - lists generated with a formula so, as mentioned previously, the list is always the same using the same seed.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 9th Feb 2008 23:34 Edited at: 10th Feb 2008 00:11
@TDK
Oh right, so it's the parameter that is too big, not the seed.
I'm going to try and make my own random number generator

Am I right in thinking when a random number is generated it is initially worth 0.0 to 1.0 and then it is multiplied by the parameter given?

[edit]
OK, I've come up with a basic version based on an algorithm from that wiki that some useful person posted

The function is pretty crude and uneconomic but I was thinking out loud
There is apparently a big hole in this algorithm, literally a hole!: if you keep going sooner or later you will get 00000 as a seed and after that the function is dead.

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 9th Feb 2008 23:44 Edited at: 11th Aug 2010 22:44
Most generation routines produce a result between 0 and 1. This value is then used to scale the supplied range value.

If DB has range of 0->32768 then you can wrap this value internally and then scale it yourself. A bit like this.

PlayBASIC code


Login to post a reply

Server time is: 2025-06-04 02:14:36
Your offset time is: 2025-06-04 02:14:36