Author Topic: About these random numbers...  (Read 5205 times)

0 Members and 1 Guest are viewing this topic.

Offline bikemadness

  • Amiga 1200
  • ****
  • Posts: 319
  • Karma: 25
  • Hard players don't go home.
    • View Profile
About these random numbers...
« on: August 25, 2007 »
they must be based on an equation, in Yabasic at least, and by powers of 10.

with the program, the first number is always zero, when given ten numbers to choose from.
with 100, another zero will be chosen, but won't write "00".
with 1000, a '1' will be chosen, and of course it will write "1"
with 10000, the fourth one will be '2', writing "12"

I noticed this with the doily's. Patterns were beginning to be familier.

U/D d-pad to scroll

Code: [Select]
open window 640,512
random=200
dim z(random)
for a=1 to random
z(a)=int(ran(1000))
next a
repeat
setdrawbuf vm
vm=1-vm
setdispbuf vm
clear window
c=peek("port1")
for a=1 to random
if a<10 b=20
if a>9 and a<100 b=10
if a>99 b=0
text 10+b,a*20+scroll,str$(a)+"."
text 60,a*20+scroll,str$(z(a))
next a
if and(c,16)>0 scroll=scroll+20
if and(c,64)>0 scroll=scroll-20
if scroll>0 scroll=0
if scroll<-(20*a)+500 scroll=-(20*a)+500
until (1=0)

Have a Yahappy day.
Have a Yahappy day.
I don't know what is wrong with the world - but I know how to fix it.

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: About these random numbers...
« Reply #1 on: August 25, 2007 »
Hmm, don't understand what your saying...
When I run that program, I'm getting random numbers... is that what you mean?
As in... the value for the array Z is random, and changes?
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline bikemadness

  • Amiga 1200
  • ****
  • Posts: 319
  • Karma: 25
  • Hard players don't go home.
    • View Profile
Re: About these random numbers...
« Reply #2 on: August 25, 2007 »
The 'random' number list does not change, each time it is run, at any time.

Have a Yahappy day.
Have a Yahappy day.
I don't know what is wrong with the world - but I know how to fix it.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: About these random numbers...
« Reply #3 on: August 25, 2007 »
I think its because you have to seed the randomization, on the time or something that will be different each time you run your program. Otherwise you end up with the same set of numbers generated each time you run your prog. I don't know how you do that in YaBasic tho.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: About these random numbers...
« Reply #4 on: August 26, 2007 »
It's a known problem, the PS2 version of yabasic always starts with the same random number seed every time you run a program.  There's no seed/srand function in yabasic, so Parabellum came up with this code
Code: [Select]
sub randomize()
  d=1 : rnd$=left$(date$,13)+left$(time$,8)
  for i=len(rnd$) to 1 step -1
    for j=d*i*asc(mid$(rnd$,i,1))/3.75 to 0 step -1
      k = ran(100)
    next
    d = 1+k/100
  next
end sub

Just call randomize() as the first thing in your program.  It uses the clock to generate a 'random' number of calls to ran before the program starts.

Jim
Challenge Trophies Won:

Offline bikemadness

  • Amiga 1200
  • ****
  • Posts: 319
  • Karma: 25
  • Hard players don't go home.
    • View Profile
Re: About these random numbers...
« Reply #5 on: August 26, 2007 »
It's known by a lot more people now, including me.
Is it a shortcut? an equation?
Because I was going to try and figure it out, if it was.

Have a Yahappy day.
Have a Yahappy day.
I don't know what is wrong with the world - but I know how to fix it.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: About these random numbers...
« Reply #6 on: August 26, 2007 »
A random number generator on a computer isn't random, it's just a very long sequence of numbers that eventually repeats itself.  For instance, in most C runtime libraries, the function is
Code: [Select]
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
or, translating that to something yabasic-ish
Code: [Select]
next=1
sub rand()
 next = next * 1103515245 + 12345
 return mod(next/65536, 32768)
end sub

Every time yabasic is started, next gets set back to 1, so the random number generator restarts the sequence again at the same place.  In C you can set 'next' by calling a function called srand().  yabasic doesn't have this function, so Parabellum's code works around it.

Jim
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: About these random numbers...
« Reply #7 on: August 26, 2007 »
This problem doesnt exist on the Yabasic disc for Playstation2 I think the system clock was used in the formula in my Tears of Spheres game it always choses the same positions and directions everytime on the PC but on run it on Playstation2 and its always random

Challenge Trophies Won: