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.

Newcomers DBPro Corner / Random Function

Author
Message
Dextro
19
Years of Service
User Offline
Joined: 26th Feb 2005
Location:
Posted: 16th Aug 2006 05:02
I have a problem that has been bugging me for quite some time now (10 years).
How do I make a truly random function that doesnt repeat numbers?

I remember once trying using 2 arrays, 1 to store a number, and a second one that would hold an "x" if that array element was already occupied, or a "_" if not.
I'm sure this is not the best way to go, so, anyone could help me out?
Specifically, I'm trying to make a Blackjack game (52 cards).
Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 16th Aug 2006 05:21
I can help more if you explain more, but I am not quite sure what you are trying to do. This displays numbers 1-52 in a random order if that is a help to you.



Dextro
19
Years of Service
User Offline
Joined: 26th Feb 2005
Location:
Posted: 16th Aug 2006 05:53
Thanks a lot, thats exactly what I was looking for
hyrichter
20
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 16th Aug 2006 08:48 Edited at: 16th Aug 2006 08:49
Just put randomize timer() at the top of your code. That seeds the random number generator with whatever number the Timer() function happens to have at that moment, so you get true randomness.

Edit:
It would be a good idea to put Randomize Timer() each time you enter into an area for calculating random numbers. The more times it gets called, the more random your numbers will be.

Good performance is better than a good excuse.
CodeSurge -- DBP Editor for serious programmers.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Aug 2006 20:03
I agree with your first sentence and disagree strongly with your second.

The randomize command just sets the seed for the number generator. If you have a function with the randomize command in and you call it several times within the same millisecond you will get the same number out every time. As machines get faster, this becomes more and more likely.

Call randomize once at the start of your program, at the start of a game, or at the start of a level. There's no point in calling it more often and a chance that you'll make it less random if you call it more often.

Login to post a reply

Server time is: 2024-09-25 07:25:10
Your offset time is: 2024-09-25 07:25:10