Rand() is not random...

Blitz3D Forums/Blitz3D Programming/Rand() is not random...

Braincell(Posted 2004) [#1]
Hi guys, maybe you could give me some thoughts on this. I make an array of a certain length and i have a for...next loop to go through the whole array and assign different random values to each element using Rand(). Each time i run this, i get the same pattern provided i do not change any parameters such as the random number range or number of elements. Rnd() does the same thing. Is there something i'm doing wrong, does something stay in memory, or does Blitz have a certain pattern which imitates a true random effect?

I have another idea of using MilliSecs() to get random values by taking the last digit of that value and performing very small waits inbetween. That would be random enough, but only 1 digit at a time, cos more would not be that random. Any thoughts?


Warren(Posted 2004) [#2]
Like most languages, you need to seed Rand() before it will produce anything resembling random numbers. Try:

SeedRnd Millisecs()

...at the start of your program.


Rob Farley(Posted 2004) [#3]
http://www.blitzbasic.com/b3ddocs/command.php?name=seedrnd


Braincell(Posted 2004) [#4]
Ahm. My thanks go to you. I wish the Blitz help file had a search function where i could type "Rnd" then i would surely see that command before bothering people over here.


joncom2000(Posted 2004) [#5]
Hmm if you put the cursor in the IDE over the start of the command when you type it in and press F1 twice it will give you the full description in the help file


Warren(Posted 2004) [#6]
I wish the Blitz help file had a search function where i could type "Rnd" then i would surely see that command before bothering people over here.

http://www.blitzbasic.com/b3ddocs/command.php?name=rnd&ref=goto

It even tells you to seed.

Go here and search for any command you like:

http://www.blitzbasic.com/b3ddocs/docs.php


Rob Farley(Posted 2004) [#7]
Or as Joncom says, press F1 twice on the command you're unsure about, they're pretty up-to-date, certainly stuff like RND hasn't changed at all for years.


Andy(Posted 2004) [#8]
>Is there something i'm doing wrong, does something stay in
>memory, or does Blitz have a certain pattern which
>imitates a true random effect?

Generally, computers produce pseudo random numbers, not true random numbers. When you use a timer as seed for the rnd function, you simply force the rnd function to begin at a different location in a table of predefined pseudo random numbers.

Andy


TartanTangerine (was Indiepath)(Posted 2004) [#9]
The fact that it is not random is a feature I use to great effect. When we generate terrains in Morphlings these are generated by "SEEDS". When multiple players are playing across the NET on a randomly generated level all we have to send to each client is the "SEED" and the levels will be identical - We do not need to send the whole level data to each client. :)


Warren(Posted 2004) [#10]
I'd be careful with that. I've been told that there are computers out there that will come up with different random numbers from the same seed. It's rare, but apparently it CAN happen...


Gabriel(Posted 2004) [#11]
It definitely can. If you want random level generation, that's fine, but predictable random level generation ( as you would need for multiplayer ) is not possible with Blitz's inbuilt random number generation.

Search for Morduun's replacement random number generation code, which is tested reliable.


Andy(Posted 2004) [#12]
>Search for Morduun's replacement random number generation
>code, which is tested reliable.

http://www.blitzbasic.com/Community/posts.php?topic=34185

Andy


Braincell(Posted 2004) [#13]
...it was kinda early morning for me... and didnt feel like reading the Rnd command reference once again. Thanks for the "press F1" clue :))


Jeremy Alessi(Posted 2004) [#14]
See how little stuff can be time consuming? Wait a year and you'll see just how many tiny roadblocks slow development down. People make some amazing things and I think by making a game yourself you'll get more respect for what other's do instead of thinking little of them for making 'small' games ;) Even the tiny projects are a challenge to do right.


Warren(Posted 2004) [#15]
That's why, in my opinion, the first thing someone should do is write a small game. Tetris, bubblet, whatever, I don't care ... just write a game and finish it. You need to see the entire process from start to end before you can realistically finish larger projects.


Braincell(Posted 2004) [#16]
LoL ok now thats pushing it :P
It was a small glitch in my brain :) resolved now. It didnt consume my time. What consumed my time you should look at the Blitz Bugs forum!! And when i first posted my first post in that thread, i had that in mind. I did, ok? I dont disrespect peoples work for christs sake. I was just asking for a few opinions saying "Ok heres one view, i know it might be wrong but i'll just be on THIS side for the sake of DIALOG" :) hope you understand.

Cheers


Braincell(Posted 2004) [#17]
Ok i wont be posting any more.


skidracer(Posted 2004) [#18]
Rob, is there any reason I shouldn't be banning you at this point?


GfK(Posted 2004) [#19]
Rob, is there any reason I shouldn't be banning you at this point?
Why would you want to do that? All he's done is break every single forum rule in the same post.
Spammed the boards with crap
Pot? Kettle?


Rob Farley(Posted 2004) [#20]
Skid, you're more than welcome to ban me if it makes you feel better.

GFK, My spamming is in humour and only in off-topic.


Andy(Posted 2004) [#21]
>Ok i wont be posting any more.

Well that'll certainly teach Rob a lesson!

In stead of feeling sorry for yourself because someone decided to ego-slap you, then why not try a different approach.

Every few weeks, there's some newbie who proclaims he'll spend the next 2 weeks writing the next big thing and wondering why everybody else seems to be unable to produce anything worthwhile...

Relax, sit back - can the attitude and participate in the community... You'll find that this is a very diverse and ultimately very accepting community. You'll fit in nicely!

Andy


Braincell(Posted 2004) [#22]
I can't imagine Rob now as anything other than a sad pathetic small creature in a dark room, angry at the whole world. I wonder why that is. He's just showing his teeth, but enough to make me sick to know he's there.

He just tries to make it harder for me to be see there are more sensible people around here who know how to have a civilised dialogue, unlike him, but i am still sure.

Speak for your sad self Robby, if anyone is insulted by what i say he'll say it to me straight and i'll apologise. But not to you.

Edit: Andy i really do want to participate in the community. I said i would need 3 years to make the potentially big thing, i was just asking why other people do not opt for that instead of smaller games...


Rob Farley(Posted 2004) [#23]
Rob now as anything other than a sad pathetic small creature in a dark room
You're quite right. I didn't realise I was so transparant.
...sensible people around here who know how to have a civilised dialogue, unlike him...
Again, spot on, I make no contribution to the community what so ever.

Listen Lenn, I wasn't trying to alienate you, just trying to ground you a little. Clearly you're biggerer and cleverererer than me so I apologise for suggesting the harbouring faeces thing.


(tu) ENAY(Posted 2004) [#24]
Rob you need to chill and rekindle your old flame of juicy bappage.


Warren(Posted 2004) [#25]
I can't imagine Rob now as anything other than a sad pathetic small creature in a dark room, angry at the whole world.

If you think Rob is something, wait until you meet Flameduck. You haven't even scratched the surface of pathetic yet...


AntonyWells(Posted 2004) [#26]
And warren isn't even drunk, he's this charming all the time!


AntonyWells(Posted 2004) [#27]
Rob, is there any reason I shouldn't be banning you at this point?


There are people far more worthy of such adulation skid.

Not that I'm a saint myself, but come on..Rob's one bad post v 2382 bad posts...


tonyg(Posted 2004) [#28]
Not sure if anybody's mentioned it but 'non-random' random numbers are good for playtesting.


Rook Zimbabwe(Posted 2004) [#29]
When multiple players are playing across the NET on a randomly generated level all we have to send to each client is the "SEED" and the levels will be identical - We do not need to send the whole level data to each client. :)
I always wondered how that worked... simple now that you think about it... That WHOOSH was the brutally simple idea going right over my head!!!


Damien Sturdy(Posted 2004) [#30]
Anyone else seeing the humors in these offences? Some of the things we say when were pissed off... heh, and Rob, first thing i could do was tell your English, by the way you said Arse. heh *chuckles*


yes, random levels are excelent over a network when you just send the seed, but the problem arises on those small few machines where the same seed generates a different level.
I suppose you could sort that by doing a "level check" at certain points of the map, if a machine fails, pop a "Machine not compatible" error up on screen :/


asdfasdf(Posted 2005) [#31]
I wish the Blitz help file had a search function where i could type "Rnd" then i would surely see that command before bothering people over here.

Add this to what ever page you want the search to be on:
<form name="goto_command" action="http://www.blitzbasic.com/b3ddocs/command.php" method="GET">
Command Name:
<input type="text" name="name" size="20" maxlength="40">
<input type="submit" value="Go">
</ul>
<input type="hidden" name="ref" value="goto">
</form>



Gabriel(Posted 2005) [#32]
Were you really so bored you had to dredge up this old topic to offer a solution which wasn't needed and is infinitely more complex than the one he was already given?


wizzlefish(Posted 2005) [#33]
I always seed rnd like this:

[code]
SeedRnd Millisecs() + (Pi/Millisecs())-Pi*Millisecs()+(Pi/Sin(4.98763))


Rook Zimbabwe(Posted 2005) [#34]
I just:
SeedRnd Millisecs()
newseed=rnd(9999)+ 911
SeedRnd Millisecs()+newseed

I am assuming that the second instance of calling the code resets the first... That way I make a more random seed. It seems to work, if I am doing this wrong I wouldlike to know.

On Rob... I find him funny... his wit is very dry and droll but I like it.

On Flameduck... Hmmm then halo is gone??? (I have been out of this board for a while!)
-RZ


fall_x(Posted 2005) [#35]
I just:

SeedRnd Millisecs()
newseed=rnd(9999)+ 911
SeedRnd Millisecs()+newseed


I am assuming that the second instance of calling the code resets the first... That way I make a more random seed. It seems to work, if I am doing this wrong I wouldlike to know.

On Rob... I find him funny... his wit is very dry and droll but I like it.

On Flameduck... Hmmm then halo is gone??? (I have been out of this board for a while!)



The random seed is also not completely random, if you run it twice on the same exact time (on different machines) then the generated random seeds will be the same, so you might as well just use Millisecs() and forget about using the second seeding.
I could be wrong tho' :)


Ross C(Posted 2005) [#36]
Are you seeding using the millisecs() value?


big10p(Posted 2005) [#37]
As far as I'm concerned, SeedRnd MilliSecs() is as good as it gets. Doing exotic things like multiplying by PI/COS/SIN etc., isn't going to make the seed any more random.


fall_x(Posted 2005) [#38]
As far as I'm concerned, SeedRnd MilliSecs() is as good as it gets. Doing exotic things like multiplying by PI/COS/SIN etc., isn't going to make the seed any more random.
My point exactly :)


Rook Zimbabwe(Posted 2005) [#39]
I sit corrected... I refuse to stand for any of you ;)
RZ