Raised This Month: $ Target: $400
 0% 

Creating random array


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 08-17-2008 , 12:33   Re: Creating random array
Reply With Quote #7

I'm a little confused. I was under the impression this was an elementary problem. *shrug*

Here was me thinking for loops were intended to iterate a predetermined (whether constant or variable) number of times. If you do insist on using the wrong keyword, there is no point endlessly increasing and decreasing the loop counter.

Code:
for(new i=1; i < 27; ) {     new index = random_num(0, sizeof(array)-1))     if(array[index] == -1)         array[index] = i++ }
I wrote what you're doing in VB.NET (because I'd forgotten how disgusting it was) and these are the number of iterations it took.

172 52 99 63 197 88 109 103 95 54 127 88 68 74 107 138 63 111 123 76

I have a suspicion that the .NET pseudorandom number generator is better in terms of uniform distribution than random_num() and so you might find it takes longer.

The way I'd achieve this is to swap random elements around (you can XOR the values if you're partial to that sort of thing). You can either call random_num() twice per swap or use modulus. It's up to you to decide how many iterations is sufficient. I'm sure you don't need me to code an example. You've probably settled for what's above anyway.
__________________
No support via PM.

Last edited by Lee; 08-17-2008 at 13:08.
Lee is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:08.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode