Random number pickup
-
- User
- Posts: 56
- Joined: Thu Jul 07, 2005 10:06 am
Random number pickup
I want to make a small apps that can pick a random number. For i.e, there are 80 people in a class. We want to divide them in to 8 group randomly. I can do like this (I post the simplest code)
NumberOfPeople=80
NumberOfGroup=8
Dim Index(NumberOfPeople)
file=CreateFile(0, "c:\list.txt")
If file
For i=1 to NumberOfPeople
Index(i) = Random(NumberOfPeople)
If Index(i) <> 0
WriteStringN(Str(Index(i)))
EndIf
Next i
CloseFile(0)
-----------
Everything seems simple, but the matter is some generated number will be duplicated to triplicated.
I don't know how to avoid this. It mean how to Random a value but not the same generated value.
Reality, as you see, the task is divide into NumberOfGroup. So, it will be wrong if there are 2 or more same number in many group.
Extensively, I want to link this thing to Database. Despite generate number, it will generate Name of the People.
Would you please help me or give me a way ? It's great if you help me with Database link.
NumberOfPeople=80
NumberOfGroup=8
Dim Index(NumberOfPeople)
file=CreateFile(0, "c:\list.txt")
If file
For i=1 to NumberOfPeople
Index(i) = Random(NumberOfPeople)
If Index(i) <> 0
WriteStringN(Str(Index(i)))
EndIf
Next i
CloseFile(0)
-----------
Everything seems simple, but the matter is some generated number will be duplicated to triplicated.
I don't know how to avoid this. It mean how to Random a value but not the same generated value.
Reality, as you see, the task is divide into NumberOfGroup. So, it will be wrong if there are 2 or more same number in many group.
Extensively, I want to link this thing to Database. Despite generate number, it will generate Name of the People.
Would you please help me or give me a way ? It's great if you help me with Database link.
As I understand it, you're looking to randomise the order of the numbers 1 to 80 (using your example). Eg. Using the numbers 1 to 4:
What you're getting:
3 3 1 2
What you want:
4 1 3 2
Here's a bit of code which should help you -i'm not going to worry about writing to a file, just assigning an array-:
You'll need to change the above code a bit (obviously
), it mightent work as is, it was just hacked up to give you the idea.
Regarding the database, can't help you unfortunately, i'm new at this thing myself
.
What you're getting:
3 3 1 2
What you want:
4 1 3 2
Here's a bit of code which should help you -i'm not going to worry about writing to a file, just assigning an array-:
Code: Select all
NumberOfPeople = 80
Dim Index(NumberOfPeople - 1) ;Remember, arrays start at 0, so array will go from 0 to 79
For i = 1 to NumberOfPeople
r = Random(NumberOfPeople) ;Assign r a value between 0 and 79
If Index(r) = 0 ;Default value, unassigned array element
Index(r) = i
Else
i = i - 1 ;Keep the value of i the same in the for loop; we picked an array element which was previously assigned
EndIf
Next i

Regarding the database, can't help you unfortunately, i'm new at this thing myself

-
- User
- Posts: 56
- Joined: Thu Jul 07, 2005 10:06 am
Really it doesn't work, but I got the point of what he meant to do. Also, check this topic about linkedlists. It may help you:
viewtopic.php?t=16135
viewtopic.php?t=16135
-
- User
- Posts: 56
- Joined: Thu Jul 07, 2005 10:06 am
- Fou-Lu
- Enthusiast
- Posts: 201
- Joined: Tue Jul 12, 2005 8:30 am
- Location: I'm pretty sure this is all a nightmare
- Contact:
I think this might work:
Got it? 
Code: Select all
Dim people(80)
for n=0 to 80
people(n)=n ;first we create an ordered list
next n
;now we make a mess!!!
for n=0 to 80
m=random(80)
temp=people(m)
people(m)=people(n)
people(n)=temp
next n

Here is one way

Code: Select all
Structure grpDef
names.s
ctr.l
EndStructure
NumPeeps = 82 ; Total People
SzGrps = 4 ; People per group
NewList allPeeps.s() ; List of the people to be grouped
NewList fullGrp.grpDef() ; List of teams or groups
For i=1 To NumPeeps ; Populate our people list somehow
AddElement(allPeeps()) ; Here we just use numbers
allPeeps()="Name_"+Str(i)
Next
ResetList(allPeeps())
AddElement(fullGrp()) ; Create the first group
While CountList(allPeeps())<>0
g=Random(CountList(allPeeps())+1) ; Get a random number 0 to remaining people+1
For i=0 To g ; Advance through the working group
NextElement(allPeeps()) ; by random number of places.
If ListIndex(allPeeps())=CountList(allPeeps())-1
ResetList(allPeeps()) ; If we reach the end of the list,
NextElement(allPeeps()) ; wrap around
EndIf
Next
fullGrp()\ctr+1 ; Up the count of people in the group
fullGrp()\names+allPeeps()+" " ; Add the name of the person
allPeeps()="" ; <<-- Just in case of string leakage
DeleteElement(allPeeps()) ; Remove the person from the people list
If fullGrp()\ctr=SzGrps ; Is the group full?
AddElement(fullGrp()) ; If so, start a new group
EndIf
Wend
FirstElement(fullGrp())
Repeat
If fullGrp()\ctr<>SzGrps
Debug Str(fullGrp()\ctr)+": "+fullGrp()\names+" ***************"
Else
Debug Str(fullGrp()\ctr)+": "+fullGrp()\names
EndIf
fullGrp()\names=""
DeleteElement(fullGrp())
FirstElement(fullGrp())
Until CountList(fullGrp())=0
@}--`--,-- A rose by any other name ..
Here are two ways, #1 with linkedlists:
Way #2 with arrays:
When you want to re-random list (or array) use goto or procedure commands. Also you can copy list into an array. Something like this :
I hope I helped a little bit
Code: Select all
NewList mylist.l()
NewList exitlist.l()
pocetak:
ClearList(mylist())
ClearList(exitlist())
For i = 0 To 80
AddElement(mylist())
mylist() = i
Next
For i = 1 To 80
brj = Random(79) + 1
SelectElement(mylist(), brj)
vst = mylist()
DeleteElement(mylist())
AddElement(exitlist())
exitlist() = vst
Next
OpenConsole()
ResetList(exitlist())
While NextElement(exitlist())
Print(Str(exitlist()) + " ")
Wend
PrintN("")
PrintN("")
ResetList(mylist())
While NextElement(mylist())
Print(Str(mylist()) + " ")
Wend
While Inkey() = ""
Wend
ClearConsole()
Goto pocetak
CloseConsole()
End
Code: Select all
Dim a(80)
For i = 1 To 80
a(i) = Random(79) + 1
For j = 1 To i
While a(i) = a(j) And i <> j
a(i) = Random(79) + 1
Wend
Next
Next
OpenConsole()
For i = 1 To 80
Print(Str(a(i)) + " ")
Next
PrintN("")
PrintN("")
PrintN("Press any key")
While Inkey() = ""
Wend
CloseConsole()
When you want to re-random list (or array) use goto or procedure commands. Also you can copy list into an array. Something like this :
Code: Select all
Dim broj.l(7)
p = 0
ResetList(exitlist())
While NextElement(exitlist())
broj(p+1) = exitlist()
p = p + 1
Wend
-
- User
- Posts: 56
- Joined: Thu Jul 07, 2005 10:06 am