RND function always gives the same sequence of numbers...

Everything related to BASIC version 1.x (Oric 1 and Atmos) or HYPERBASIC (Telestrat).
Don't hesitate to give your small program samples, technical insights, or questions...
Brana
Flying Officer
Posts: 169
Joined: Fri Nov 30, 2007 8:30 pm
Contact:

RND function always gives the same sequence of numbers...

Post by Brana »

If I launch the Euphoric Emulator and do the simple:

PRINT INT(1+RND(1)*1000)

I will always get the different number...

Hovewer, If I make a simple BASIC program as this one:

3 FOR A = 1 TO 10
4 S = INT (1+RND(1)*1000)
5 PRINT S
6 NEXT A

and save it as a TAP file,
then If I doubleclick on that file from within the My Computer, Euphoric Emulator will launch and run my program, but in this case IT WILL ALWAYS GIVE THE SAME SEQUENCE OF NUMBERS!?

Why is this happening?

Is there anything I could do to have DIFFERENT sequence of numbers generated by RND function in the BASIC program that is started by a doubleclick of the mouse from My Computer?
Antiriad2097
Flying Officer
Posts: 158
Joined: Tue May 09, 2006 9:42 pm
Location: Aberdeen, UK
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Antiriad2097 »

The latter is true of the real hardware, it always generates the same list of 'random' numbers from a given seed.

I'm not sure why your first example is giving true random results.
User avatar
Symoon
Archivist
Posts: 2301
Joined: Sat Jan 14, 2006 12:44 am
Location: Paris, France

Re: RND function always gives the same sequence of numbers..

Post by Symoon »

It's because, when you run Euphoric directly with your program, Euphoric is always in the exact same state so the random function will always give the same results. This probably doesn't happen with real hardware, nor if Euphoric works a bit before running the program - though I don't exactly know what is used by the random function to, obviously, simulate random.
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Chema »

I think you are right Symoon. All the procedural random generators which start from a given seed act like that. Will probably happen on the real hardware too, unless some kind of source is used to generate the seed (such as VIA timers). But even with that, I doubt it will change anything when using emulators, as the state is always the same.

My latest random generator uses the VIA and other quite random bits, but you'll have the same sequence with euphoric unless you have some kind of user interaction (which changes timings between calls).

Oh, and even being able to change the seed by program (don't know if this is possible, but should be by pokeing somehting into an address), that won't help Brana. He will have the same thing happening again and again unless he does something like a loop asking for a keypress and keep calling RND(1) inside the loop. At least the sequence will start at a different position...

I wonder if something could be done if Euphoric is emulating a hardware real-time clock... :roll:
Brana
Flying Officer
Posts: 169
Joined: Fri Nov 30, 2007 8:30 pm
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Brana »

Remember my short program from the beginning of this post?

3 FOR A = 1 TO 10
4 S = INT (1+RND(1)*1000)
5 PRINT S
6 NEXT A

If I add the following line at the beginning of the program:

2 INPUT "Please input some random word here";WRD$

so the program looks like this:

2 INPUT "Please input some random word here";WRD$
3 FOR A = 1 TO 10
4 S = INT (1+RND(1)*1000)
5 PRINT S
6 NEXT A

This way the typing speed of the user and the number of keystrokes that he would make - would contribute the different state of the Emulator, right?

But no - even in this case - I STILL GET THE SAME SEQUENCE OF NUMBERS!?

P.S. I DO emulate a real-time clock in Euphoric.ini file... :)
Igotafro
Officer Cadet
Posts: 47
Joined: Tue Feb 05, 2008 3:42 pm

Re: RND function always gives the same sequence of numbers..

Post by Igotafro »

You're using RND(1), that is, RND with a seed of 1. It should always generate the same sequence of numbers.
What you need is a way of getting a random number to replace the 1.
Just make a loop which increases a variable and at the same time waits for the user to press a key.
Then use that variable instead of 1 in RND(1).
Antiriad2097
Flying Officer
Posts: 158
Joined: Tue May 09, 2006 9:42 pm
Location: Aberdeen, UK
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Antiriad2097 »

As I suggested earlier, the real hardware does the same thing.

The RND function will generate the same list of numbers, starting at the same point.

iirc, you can use RND (1) for example, then RND (3), but if you go back to RND (1) it'll start again with the same list of numbers from the beginning (I think, its been a while)

In the example you'd given, inputting a word doesn't change your RND statement. If you asked for a number and used that as your seed, you'd get variable results (though the same one if the same number was entered at the start).

Simpler to run a loop until a key is pressed and use it to either run through your RND function in the loop so its start point is a variable position through the list (depending on where the user stopped it), or count the number of loops and use that number as the seed for your RND following.
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Chema »

I am not sure that the Oric's RND function works like that. In the Oric-1 manual it says:
RND(n) will produce a random number greater than or equal to 0
and less than 1, if n is a positive number. If n is a negative number,
then the random seed is set to a particular number, and subsequent
positive n’s will always produce the same sequence. If n is zero, the
last random number generated will be produced.
So there's no difference in RND(1) or RND(3). And I even did not know that a negative number set the seed.

Anyway, still the problem is not solved.

But, can't a Real-time clock extension be emulated with Euphoric? I know nothing about it, but if you can get the seconds (or milliseconds) or the current time of day by peeking somewhere, you can use that as a seed to initialize the RND generator differently in every run... could that be done?

EDIT: Forgot to copy the usage of RND(0) in the quoted text
Brana
Flying Officer
Posts: 169
Joined: Fri Nov 30, 2007 8:30 pm
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Brana »

Yes, and they are:

PEEK (869) = date (example 31)
PEEK (868) = month (example 10)
PEEK (870) = Year (example 12 for 2012)
PEEK (865) = Hour, and
PEEK (866) = minute

But, how would I integrate it in to the RND function?

Would something like this:

4 S = INT (1+RND(PEEK(866))*1000)

solve the problem?
Antiriad2097
Flying Officer
Posts: 158
Joined: Tue May 09, 2006 9:42 pm
Location: Aberdeen, UK
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Antiriad2097 »

Chema wrote:So there's no difference in RND(1) or RND(3).
My bad, must be mixing up my BASICs. I know one of them resets to the same point if you change the seed, maybe that was Speccy.

I remember having to work round the lack of truly random function using loops until a key was pressed to work to a random point in the list.
User avatar
Chema
Game master
Posts: 3013
Joined: Tue Jan 17, 2006 10:55 am
Location: Gijón, SPAIN
Contact:

Re: RND function always gives the same sequence of numbers..

Post by Chema »

Brana wrote:
Would something like this:

4 S = INT (1+RND(PEEK(866))*1000)

solve the problem?
Well it would need a try. Not having also a seconds counter is a problem (you'll get the same value during a whole minute), but I'd do something like:

1 RND(-PEEK(866))

Which should reset the seed to a "position" related to the minute of day.

Or, as Antiriad said, use a loop which waits for a keypress and which calls RND inside. Or combine both...

Code: Select all

10 REPEAT
20 I=RND(1)
30 UNTIL KEY$<>""
40 RND(-PEEK(866)+I)
Not sure the above code is correct, but you should get the idea. Also you can add in line 40 the value of one of the VIA counters (for instance at $308, by PEEK(#308) ) to add more sources of randomness :)
Post Reply