Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Rnadom Numbers.. (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Listbox Question
Next thread: possible to permanently cast to parent?


Win32 API Declares   -   Berg, Heath
  Rnadom Numbers..   -   tom.russell transport.alstom.com
   Re: Rnadom Numbers..   -   dda
   Re: Rnadom Numbers..   -   Noel Henson
    Re: Rnadom Numbers..   -   Joseph J. Strout
    Re: Rnadom Numbers..   -   Joseph J. Strout
     Re: Rnadom Numbers..   -   Noel Henson
   Re: Rnadom Numbers..   -   Lars Jensen
    Re: Rnadom Numbers..   -   Joseph J. Strout
     Re: Rnadom Numbers..   -   Jim Payne
   Re: Rnadom Numbers..   -   Charles Yeomans
    Re: Rnadom Numbers..   -   Joseph J. Strout
    Re: Rnadom Numbers..   -   Phil M
   Listbox Question   -   Joe Monia

Rnadom Numbers..
Date: 01.08.05 17:50 (Mon, 1 Aug 2005 12:50:43 -0400)
From: tom.russell transport.alstom.com
Im loading a listbox with Random numbers like so:

Dim r as New Random
Dim i,j,k as Integer


for i = 1 to Listbox1.ListCount
listBox1.Cellt(i,2) = Str(r.InRange(1,22))
listBox1.Cell(i,3) = Str(r.InRange(23,43))
next

How can I make it so either col doesnt get 2 of the same numbers....so if
I have say 23 rows in both cols then each cell should be different, no 2
alike in each column.

Any ideas?

Thanks,

Tom

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Rnadom Numbers..
Date: 01.08.05 18:31 (Mon, 1 Aug 2005 19:31:20 +0200)
From: dda
dim i,j,n As Integer
dim d As new Dictionary
dim r as new Random

j=// Number of desired numbers
if j=0 Then Return
for i=1 to 255 // assuming you want numbers between 1 and 255.
Adjust to needs.
d.Value(i)=i
next

lb1.DeleteAllRows
for i=1 to j
n=r.InRange(1,d.Count)
lb1.AddRow Str(d.Value(d.Key(n-1)))
d.Remove d.Key(n-1)
Next

Voilà! :-)

-

Re: Rnadom Numbers..
Date: 01.08.05 18:33 (Mon, 1 Aug 2005 10:33:42 -0700)
From: Noel Henson
On Monday 01 August 2005 09:50 am, <email address removed> wrote:
> Im loading a listbox with Random numbers like so:
>
> Dim r as New Random
> Dim i,j,k as Integer
>
> for i = 1 to Listbox1.ListCount
> listBox1.Cellt(i,2) = Str(r.InRange(1,22))
> listBox1.Cell(i,3) = Str(r.InRange(23,43))
> next
>
> How can I make it so either col doesnt get 2 of the same numbers....so
> if I have say 23 rows in both cols then each cell should be different,
> no 2 alike in each column.
>
> Any ideas?
>

You will need to create an array of the numbers you wish to use and do
something like this:

dim deck(44) as integer
dim i,n,x,y as integer
dim r as new random

for i = 0 to 43
deck(i) = i - 1
next

' now shuffle it

for i = 1 to 100 ' or whatever
x = r.InRange(1,22)
y = r.InRange(1,22)
n = deck(x)
deck(x) = deck(y)
deck(y) = n

x = r.InRange(23,44)
y = r.InRange(23,44)
n = deck(x)
deck(x) = deck(y)
deck(y) = n
next

for i = 1 to Listbox1.ListCount
listBox1.Cellt(i,2) = str(deck(i))
listBox1.Cell(i,3) = Str(deck(i+22))
next

Noel

Re: Rnadom Numbers..
Date: 01.08.05 18:18 (Mon, 1 Aug 2005 11:18:53 -0600)
From: Joseph J. Strout
At 12:50 PM -0400 8/1/05, <email address removed> wrote:

> for i = 1 to Listbox1.ListCount
> listBox1.Cellt(i,2) = Str(r.InRange(1,22))
> listBox1.Cell(i,3) = Str(r.InRange(23,43))
> next
>
>How can I make it so either col doesnt get 2 of the same numbers....so if
>I have say 23 rows in both cols then each cell should be different, no 2
>alike in each column.

Well, probably the easiest way is to just build an array of choices
and then Shuffle them. No need for the Random class at all in this
case.

HTH,
- Joe

Re: Rnadom Numbers..
Date: 01.08.05 18:32 (Mon, 1 Aug 2005 11:32:34 -0600)
From: Joseph J. Strout
At 10:33 AM -0700 8/1/05, Noel Henson wrote:

>' now shuffle it
>
>for i = 1 to 100 ' or whatever
> x = r.InRange(1,22)
> y = r.InRange(1,22)
> n = deck(x)
> deck(x) = deck(y)
> deck(y) = n
>
> x = r.InRange(23,44)
> y = r.InRange(23,44)
> n = deck(x)
> deck(x) = deck(y)
> deck(y) = n
>next

This code is no longer necessary; just use deck.Shuffle instead.

Best,
- Joe

Re: Rnadom Numbers..
Date: 01.08.05 18:54 (Mon, 1 Aug 2005 10:54:03 -0700)
From: Noel Henson
On Monday 01 August 2005 10:32 am, Joseph J. Strout wrote:
>
> This code is no longer necessary; just use deck.Shuffle instead.
>
> Best,
> - Joe

Cooool.

What algorithm does deck.shuffle use?

Noel

Re: Rnadom Numbers..
Date: 01.08.05 18:42 (Mon, 01 Aug 2005 13:42:35 -0400)
From: Lars Jensen
> How can I make it so either col doesnt get 2 of the same numbers....so if
> I have say 23 rows in both cols then each cell should be different, no 2
> alike in each column.

I would write a function to create and return an array of N random numbers
(by checking each new element against all existing elements before appending
it). I would then call this routine once before my loop, and index into the
result during the loop.

lj
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Rnadom Numbers..
Date: 01.08.05 18:57 (Mon, 1 Aug 2005 11:57:20 -0600)
From: Joseph J. Strout
At 10:54 AM -0700 8/1/05, Noel Henson wrote:

> > This code is no longer necessary; just use deck.Shuffle instead.
>
>Cooool.
>
>What algorithm does deck.shuffle use?

It does a selection shuffle, using the Rnd function as its underlying PRNG.

Best,
- Joe

Re: Rnadom Numbers..
Date: 01.08.05 19:45 (Mon, 01 Aug 2005 13:45:04 -0500)
From: Jim Payne
Joseph J. Strout wrote:

> At 10:54 AM -0700 8/1/05, Noel Henson wrote:
>
>> > This code is no longer necessary; just use deck.Shuffle instead.
>>
>> Cooool.
>>
>> What algorithm does deck.shuffle use?
>
> It does a selection shuffle, using the Rnd function as its underlying
> PRNG.
>
> Best,
> - Joe
>
Joe:

Any way we can set the seed for the Rnd function, thus allowing
reproduction of a random hand?

Jim Payne

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Rnadom Numbers..
Date: 01.08.05 20:07 (Mon, 1 Aug 2005 15:07:04 -0400)
From: Charles Yeomans

On Aug 1, 2005, at 2:45 PM, Jim Payne wrote:

> Joseph J. Strout wrote:
>
>> At 10:54 AM -0700 8/1/05, Noel Henson wrote:
>>
>>> > This code is no longer necessary; just use deck.Shuffle instead.
>>>
>>> Cooool.
>>>
>>> What algorithm does deck.shuffle use?
>>
>> It does a selection shuffle, using the Rnd function as its underlying
>> PRNG.
>>
>> Best,
>> - Joe
>>
> Joe:
>
> Any way we can set the seed for the Rnd function, thus allowing
> reproduction of a random hand?

For that I think you can use the Random object.

--------------
Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Rnadom Numbers..
Date: 01.08.05 20:36 (Mon, 1 Aug 2005 13:36:27 -0600)
From: Joseph J. Strout
At 1:45 PM -0500 8/1/05, Jim Payne wrote:

>>It does a selection shuffle, using the Rnd function as its underlying PRNG.
>
>Any way we can set the seed for the Rnd function, thus allowing
>reproduction of a random hand?

No, I'm afraid not. For that you'd have to code your own shuffle.

Best,
- Joe

Re: Rnadom Numbers..
Date: 01.08.05 22:01 (Mon, 1 Aug 2005 23:01:25 +0200)
From: Phil M
On Aug 1, 2005, at 9:36 PM, Joseph J. Strout wrote:

>>> It does a selection shuffle, using the Rnd function as its
>>> underlying PRNG.
>>
>> Any way we can set the seed for the Rnd function, thus allowing
>> reproduction of a random hand?
>
> No, I'm afraid not. For that you'd have to code your own shuffle.

Try something like this:

// somewhere in your program
Rand = New Random

// Variables, and setup values for deck
Dim k As Integer
Dim deck(51) As Integer

For k = Rand.InRange(1, 12) DownTo 0
deck.Shuffle
Next

I think that Shuffle is somewhat expensive, so you don't want to do too
many but you get the idea.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>