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 numbers.

Author
Message
DBLearner
20
Years of Service
User Offline
Joined: 15th Jun 2004
Location: UK
Posted: 15th Jun 2004 23:02
Hi. I'm trying to make a lottery program but I can't stop the program from comming up with the number 0 or duplicates.
Could anyone help please?
I've put the full coding underneath.

Adam
BearCDPOLD
21
Years of Service
User Offline
Joined: 16th Oct 2003
Location: AZ,USA
Posted: 16th Jun 2004 06:25
Somewhere before you start creating random numbers you need to put a RANDOMIZE command. The only parameter RANDOMIZE requires is a single number that helps it start from somwhere when it randomizes. The easiest way is to just put RANDOMIZE TIMER().
Test the code in the source button and it will be random, all I did was put RANDOMIZE TIMER() below CLS.

Crazy Donut Productions, Current Project: KillZone
Web Site Button Does Not Work, Visit Here: http://www.geocities.com/crazydonutproductions/index.html
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Jun 2004 13:00
The way to approach this kind of problem is like so...

Create an array of 49 elements. Put the numbers 1 to 49 in the elements.

Using the Randomize advice above, create a loop that runs a considerable number of times (10,000 or so, it will run in no time).

Within the loop, generate pairs of random numbers between 1 and 49. Swap the 2 elements indicated by these pairs. You are not generating new numbers, simply moving the existing numbers around in your array. So you will never get duplicates.

Finally, select the first six elements of the array for your lottery numbers.

This theory works for shuffling cards and other similar tasks.

BatVink
http://facepaint.me.uk/catalog/default.php AMD 3000+ Barton, 512Mb Ram, 120 Gig Drive space, GeForce 5200 FX 128 Mb, Asus A7N8X Mobo.
Terms & Conditions apply
DBLearner
20
Years of Service
User Offline
Joined: 15th Jun 2004
Location: UK
Posted: 2nd Jul 2004 01:48
Hi All.

I've put randomize timer() below CLS and I still keep getting 0 in my lottery numbers.

Regards to the second post, I'm sorry to say I'm just learning and I don't understand what that post means.

The bit that says "Create an array of 49 elements. Put the numbers 1 to 49 in the elements." I think I've done but the bit that says
"Within the loop, generate pairs of random numbers between 1 and 49. Swap the 2 elements indicated by these pairs. You are not generating new numbers, simply moving the existing numbers around in your array. So you will never get duplicates.

Finally, select the first six elements of the array for your lottery numbers.

This theory works for shuffling cards and other similar tasks." I don't understand.

(Sourse code is under 1st post)

Could anyone help please?
Thanks
Adam

Adam
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 2nd Jul 2004 04:34 Edited at: 2nd Jul 2004 04:35
The way to select the 6 random values is to first realize that the RND() routine returns a number from 0 to the number given it. Thus; rnd(50) will be a value from 0 to 50. So you have to make the rnd() command something like;
X=RND(48)+1
for the range of 1 to 49.

Next we come to repeats. Sadly, the only way to avoid repeats is to use if statements, like this;
A = rnd(48) +1
loop1:
B = rnd(48)+1
if b = a then goto loop1
loop2:
C = rnd(48)+1
if c = a then goto loop2
if c = b then goto loop2
...

And so on.

If you do not understand matrixes, this is the only way to get there...

S.

Any truly great code should be indisguishable from magic.
DBLearner
20
Years of Service
User Offline
Joined: 15th Jun 2004
Location: UK
Posted: 6th Jul 2004 23:41
Hi All,
I've done all of the above apart from the bit that avoids duplicates. The reason why I haven't done the duplicates bit is because I'm still getting 0 and I can't figure out why?
I got rid of

and replaced it with:

There seems to be no difference between the 2 commands.
Also, the
command, I can't see a difference whether it's there or not.
Can anyone help please?
I've put the sourse code to the whole program below.
Thanx
Adam
[email protected]

Adam
DBLearner
20
Years of Service
User Offline
Joined: 15th Jun 2004
Location: UK
Posted: 6th Jul 2004 23:44
Also, if you use any sourse code to explain things, could you convert it to sourse code because some times I can't tell the difference between English explinations and sourse code examples.
Eg... for the range of 1 to 49. which is 2 posts up.
Thanx
Adam
[email protected]

Adam
Doom weaver
20
Years of Service
User Offline
Joined: 5th Jul 2004
Location:
Posted: 7th Jul 2004 19:38 Edited at: 7th Jul 2004 19:39
Its quite simple you need 6 random numbers right

`Declare the variable 'value' with six parts to it
dim value(6)

`six cards require the function to be repeated 6 times
for card = 1 to 6
`this makes the value of the card you are up to between 1 and 50 i.e. value(1)=27, then when it repeats value(2)=4 and etc.
value(card)=rnd(49)+1
next card

so now say you want to print all those values
your could write

print value(1)
print value(2)
print value(3)
print value(4)
print value(5)
print value(6)

or

for card = 1 to 6
print value(card)
next card
Doom weaver
20
Years of Service
User Offline
Joined: 5th Jul 2004
Location:
Posted: 7th Jul 2004 19:42
what this is doing;
FOR T=1 TO 49
01=RND(T)
02=RND(T)
03=RND(T)
04=RND(T)
05=RND(T)
06=RND(T)
next T
is on the first run making
01=RND(1)
02=RND(1)
etc.
and on the fifth run making
01=RND(5)
02=RND(5)
etc.

actually you could make that code work fine by getting rid of the for-next and instead of rnd(t) make it rnd(49)+1
Doom weaver
20
Years of Service
User Offline
Joined: 5th Jul 2004
Location:
Posted: 7th Jul 2004 19:47
and make sure you put RETURN at the end of each gosub label.
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 8th Jul 2004 05:02
umm!, what you need to know is that rnd gives a number from zero to the number you give it, so to get a random number between one and 48 you need to do

number=rnd(47)+1 <<<< note that +1...it`s important

so the highest number you can get is 47, and 47+1=48, likewise the lowest number you can get is 0, and 0+1=1, so that solves that problem.
your second problem is that you don`t want any of the numbers to repeat, you can do this in two ways, firstly, you can chose a number and check to make sure it isn`t already picked, for that you need to remember the last numbers you picked and compare them all with the number you already have, and only pick the next number when they all have been checked and passed the test.
the other way is to have an array of numbers from 1 to 48, so that

number(1)=1
number(2)=2
etc
.
.
number(48)=48

then shuffle all the numbers in the 48 locations by chosing two at random, then swapping the numbers over, so that after a few thousand swaps the array looks something like

number(1)=17
number(2)=35
etc
.
.
number(48)=2

once you have them all at random then you can just pick the first numbers, since they where all just shuffled and not copied, you won`t get any duplicates, you can shuffle the values like this

dim Number(48)
for i=1 to 48
Number(i)=i
next i

for i=1 to 1000
Location_1=(rnd(47))+1
Location_2=(rnd(47))+1
Temp=Number(Location_1)
Number(Location_1)=Number(Location_2)
Number(Location_2)=Temp
next i

print "after shuffle (note no repetitions)"
for i=1 to 48
print "location ";i;" = ";Number(i)
next i
do
loop

will create 48 numbers and shuffle them for you, you don`t even need to check for the swap numbers being the same since all that does is leave a number where it is (it swaps it with it`self), hope thats some help to you.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, Nvidia FX5900 gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster.

Login to post a reply

Server time is: 2025-05-25 13:22:11
Your offset time is: 2025-05-25 13:22:11