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 / Need help with randomization...rnd(n); randomize n; randomize timer()

Author
Message
Brazos
16
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 8th Apr 2011 21:25
I have searched and read in the forum about this subject but am still confused as to just how it works. I've wrote a program I want to use as a screen saver. It will randomize for a while and then get stuck for a while and then go back to being random. I would appreciate all the help anyone would give. Here is the code:

29 games
13
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 8th Apr 2011 23:39
Hi Brazos

I've had a look at your code and the good news is that you're using the RANDOMIZE, TIMER() and RND commands in the correct way.

When I ran your program what I saw was the screen split into a grid, lots of random patterns filling the screen. Then a part where one pattern would be pasted to the screen starting top left, filling to the right before dropping to the next line. I'm guessing that's what you meant when you said it "... then get stuck for a while". The pattern would then go back to being random.

This is not a caused by your use of the RANDOMIZE command.

The problem is in the sub routine "place_boxes", specifically the if statement:



Basically, it pastes the same image on the screen (bitmap 0 by default) from top left, fills the screen to the right and then drops down a line, i.e. the bit when the program "gets stuck".

I changed the if statement to set the current bitmap to 1 at the start of the if statement then set it back to 0 (i.e. the screen) at the end of the if statement.



Bitmap 1 is invisible so it does all the pasting unseen.

I used bitmap 1 as you created this at the start of the program and never deleted it, so it was availalbel for use.

I also remarked out the "wait 1" as I thought this slowed things up.

I have to admit that I don't really know what half your code is doing (including the bit that I edited), it might be helpful if you commented your code.

Apart from that, I thought it was a very cool screen saver.

Hope this helps.
Latch
13
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Apr 2011 23:57 Edited at: 9th Apr 2011 00:07
Hi Brazos,

The funny thing about numbers generated with rnd() is that they aren't actually random. Let me make a little more sense of this.

When you call value=rnd(some number) , some number is the highest number that can be returned and the lowest number is 0 . The numbers are generated by an algorithm that makes it SEEM like they are random. In actuality, the number generator is creating a series of numbers.

Wouldn't that mean the same numbers are repeated every time you run the program? Actually, yes it does. This is where RANDOMIZE comes into play.

In order for the same series of numbers not to be repeated over and over each time you run your program, you have to SEED the random number generator. Seeding gives the algorithm a parameter from which to calculate it's series. You seed the random number generator by using the RANDOMIZE <seed value> function.

If you seed the random number generator with the same value, the same series of numbers will be generated over and over again:



Now, you often see a bit of code that looks like this:

randomize timer()

The timer() function constantly counts up by 1/1000 of second from the time you start windows. When you use timer() to seed the random number generator, it is very likely that you will have a different seed each time you run your program. And with a different seed, you will get a different series of generated numbers.

It usually suffices to only seed the generator once at the beginning of your program. The random number generator will create a series that seems random. The only time this series is repeated is when you use the same seed value. So seeding once is usually all that is required because a single series won't repeat.

Now, sometimes, you want to seed the generator with the same value. Say, for example, you always wanted to generate the same shaped matrix using RANDOMIZE MATRIX etc. RANDOMIZE MATRIX isn't a seeding for the rnd generator so don't get confused with the term RANDOMIZE (they should've used a different keyword for that) . Anyway, you would seed the random number generator with the same value before reshaping the matrix:



Enjoy your day.
Brazos
16
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 9th Apr 2011 02:00
Thanks for posting back.

29 games, I intended for the program to stop there to give the viewers eyes a rest. What I ment by it "gets stuck" was that it seems to lock on to selecting one pattern over and over after the program runs awhile. Then it moves on to selecting diffenent pattern (the last subroutine in my code).

Latch, why aren't you a moderator yet? Your post are always so informative. I used randomize timer() and rnd(num) in my code. The last subroutine (seen below) in my code does not seem to select a random number each time.

Brazos
16
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 9th Apr 2011 02:34
Here is the code with "rems":

Brazos
16
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 9th Apr 2011 03:38
I think I found the problem. It was with this portion of the code:



My program became more random when I changed the following:



The program would get "hung up" on the "mixitup" random number.
Sorry to triple post, but I thought I should explain the solution.
29 games
13
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 9th Apr 2011 10:18
Ah, I get it now. I misunderstood what the program was meant to be doing.

The one thing that I learned when using random numbers is to expect the same number to appear two or three times (or more) in succession.

Because of this, random things sometimes don't appear random.

What you might want to do is add a bit into your code so it can't select the same pattern twice in a row. This may make it appear more random.

Anyway, glad you sorted it out.
Latch
13
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Apr 2011 19:17
One other thing, in DBC the maximum value that can be returned using rnd() is 32767. Even if you did something like rnd(1000000) the highest value returned would be 32767. Here's a quick test of that:



You could expand the apparent limit with some simple math(s):



Enjoy your day.

Login to post a reply

Server time is: 2019-08-25 16:42:51
Your offset time is: 2019-08-25 16:42:51