Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: How to randomize ?  (Read 3291 times)

0 Members and 1 Guest are viewing this topic.

Tarzin

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 35
    • Ami-France
How to randomize ?
« on: November 10, 2007, 04:08:08 PM »

Hello,

I'd like to randomize 96 numbers on a variable but without having the same value

Easy way:
dim piece(96)
for i=1 to 96: piece(i)=rnd(96):next i

but I'll have the same value on many variables.
Ayy idea ??

Thanks a lot for your help

Ps: this is needed for the game I'm trying to develop.
Logged
Power User! :-)
A500/1Mo + 2nd floppy drive

WinUAE 1.5.x / OS 3.9 BB2 / Amikit 1.x.
http://amifrance.hw.tc/

AmiGame, Ultimate FrontEnd for WinUAE!
http://www.amigame.fr/

Spellcoder

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 27
    • Amiga Coding WIKI
Re: How to randomize ?
« Reply #1 on: November 10, 2007, 04:39:11 PM »

If you don't use very high values and only need a small list numbers,
the fastest/easyest approach would probably be to have an array in which you flag which numbers are used.
Like this:

HIGHEST_VALUE = 200
AMOUNT = 96

Dim NumberUsed(HIGHEST_VALUE)
Dim MyList(AMOUNT)

For I=0 To HIGHEST_VALUE
  NumberUsed(I)=False
Next I

For I=1 to AMOUNT
GetNew:
   NewNumber = rnd(HIGHEST_VALUE)
   If NumberUsed(NewNumber)=True Then Goto GetNew
   NumberUsed(NewNumber)=True
   MyList(I) = NewNumber
Next I


Make sure you don't get more random numbers than HIGHEST_NUMBER,
or the program will freeze. (it will retry getting new numbers, but since
all numbers are used up it will retry forever :P)
« Last Edit: November 10, 2007, 04:40:44 PM by Spellcoder »
Logged

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com
Re: How to randomize ?
« Reply #2 on: November 10, 2007, 08:44:57 PM »


I think I read somewhere about being able to generate *exactly the same* random numbers each time a game was played. I'm not sure, but I think it involved negative numbers. Anyone recall anything like that?


Regards,
Lonewolf10

Logged

Spellcoder

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 27
    • Amiga Coding WIKI
Re: How to randomize ?
« Reply #3 on: November 10, 2007, 09:11:28 PM »

According to the AMOSPro manual, the random numbers are generated
with the given seed (with the Randomize function). If you didn't use
the Randomize <seed> then the default seed is used.

If you set your own seed value you should get the same
values back each time.

Randomize 8080
For I=1 to 10
   Print rnd(100)
Next I

Also using a negative number with rnd() gives back the previous result.
(the manual says this is helpfull for debugging, but I don't see why)

If the code above doesn't work, try using negative numbers with rnd().
It could be that the manual isn't right and that the seed is only used
when you use a negative number. (might have read this somewhere,
but havn't tested it)
« Last Edit: November 10, 2007, 09:17:35 PM by Spellcoder »
Logged

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: How to randomize ?
« Reply #4 on: November 11, 2007, 02:27:05 AM »

@Eric

It sounds to me like you are trying to do a shuffle play.

To accomplish that so that each number is used exactly once try this code:

Code: [Select]
dim a(96)

` initialize ordered list
for count=0 to 95
    a[count]=count
next count
` prepare list for shuffle play
for count=0 to 95
    temp=a[count]
    b=rnd(96)
    a[count]=a[b]
    a[b]=temp
next count
Logged

Tarzin

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 35
    • Ami-France
Re: How to randomize ?
« Reply #5 on: November 12, 2007, 09:39:05 AM »

@spellcoder, thanks for your help!

@SamuraiCrow
You were right, that this piece of code that I need. Works fine!

And yes I'm working on a board game which needs to random pieces on it.
I think I'll open a special thread to present my game. It's an old projet that was firstly started with Stos and then converted to Amos (at least 15 years ago!)


Logged
Power User! :-)
A500/1Mo + 2nd floppy drive

WinUAE 1.5.x / OS 3.9 BB2 / Amikit 1.x.
http://amifrance.hw.tc/

AmiGame, Ultimate FrontEnd for WinUAE!
http://www.amigame.fr/
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022