 |
|
|
|
| Author |
Message |
Stein Lundbeck
Joined: 04 Oct 2007 Posts: 1
|
Posted: Tue Apr 25, 2006 2:04 pm Post subject: Problems with the Rnd() function |
|
|
Hi,
I want to generate a number between 41 and 71.
To do this I use the following syntax:
Randomize()
CInt(Int((71 * Rnd()) + 41))
I wrote the following function just to test this:
Private Sub TestRnd()
Randomize()
Dim i As Int32 = 0
Do While i < 50
Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
i += 1
Loop
End Sub
These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
As you can see, there are many numbers that is not between 41 and 71. Even
numbers with 3 digits!
So what I am doing wrong ?
BR,
Stein
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Gary Chang[MSFT]
Joined: 04 Oct 2007 Posts: 5
|
Posted: Tue Apr 25, 2006 1:27 pm Post subject: RE: Problems with the Rnd() function |
|
|
Hi Stein,
>I want to generate a number between 41 and 71.
I suggest you use the following expression, it should work:
CInt(Int((31 * Rnd()) + 41))
Thanks!
Best regards,
Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights. |
|
| Back to top |
|
 |
Jan Hyde
Joined: 04 Oct 2007 Posts: 1211
|
Posted: Tue Apr 25, 2006 2:41 pm Post subject: Re: Problems with the Rnd() function |
|
|
"Stein Lundbeck" 's wild
thoughts were released on Tue, 25 Apr 2006 10:04:28 +0200
bearing the following fruit:
>Hi,
>
>I want to generate a number between 41 and 71.
>To do this I use the following syntax:
As far as I can tell that is not VB6 code, if your
programming VB.Net then try a dotnet group.
Although many commands have the same name between the two
languages, they often work differently.
J
>Randomize()
>CInt(Int((71 * Rnd()) + 41))
>
>
>I wrote the following function just to test this:
>
>Private Sub TestRnd()
> Randomize()
>
> Dim i As Int32 = 0
>
> Do While i < 50
> Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
>
> i += 1
> Loop
> End Sub
>
>
>These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
>97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
>
>As you can see, there are many numbers that is not between 41 and 71. Even
>numbers with 3 digits!
>
>So what I am doing wrong ?
>
>BR,
>Stein
>
Jan Hyde (VB MVP)
--
Rich foods are like destiny. They, too, shape our ends. (Art. Moger) |
|
| Back to top |
|
 |
Jan Hyde
Joined: 04 Oct 2007 Posts: 1211
|
Posted: Tue Apr 25, 2006 2:43 pm Post subject: Re: Problems with the Rnd() function |
|
|
v-garych@online.microsoft.com ("Gary Chang[MSFT]")'s wild
thoughts were released on Tue, 25 Apr 2006 09:27:06 GMT
bearing the following fruit:
>Hi Stein,
>
>>I want to generate a number between 41 and 71.
>
>I suggest you use the following expression, it should work:
>
>CInt(Int((31 * Rnd()) + 41))
>
It would really help if MS employees directed questions to
the correct group otherwise it's going to send out confusing
signals to posters.
J
>Thanks!
>
>Best regards,
>
>Gary Chang
>Microsoft Online Community Support
>==================================================
>When responding to posts, please "Reply to Group" via your newsreader so
>that others may learn and benefit from your issue.
>==================================================
>This posting is provided "AS IS" with no warranties, and confers no rights.
Jan Hyde (VB MVP)
--
Rich foods are like destiny. They, too, shape our ends. (Art. Moger) |
|
| Back to top |
|
 |
Stein Lundbeck
Joined: 04 Oct 2007 Posts: 1
|
Posted: Tue Apr 25, 2006 6:59 am Post subject: RE: Problems with the Rnd() function |
|
|
Thanks Gary,
it worked!
But I cant figure out why
BR,
Stein
""Gary Chang[MSFT]"" wrote:
> Hi Stein,
>
> >I want to generate a number between 41 and 71.
>
> I suggest you use the following expression, it should work:
>
> CInt(Int((31 * Rnd()) + 41))
>
>
> Thanks!
>
> Best regards,
>
> Gary Chang
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
> |
|
| Back to top |
|
 |
Stein Lundbeck
Joined: 04 Oct 2007 Posts: 1
|
Posted: Tue Apr 25, 2006 7:03 am Post subject: Re: Problems with the Rnd() function |
|
|
Sorry Jan,
I didnt know I couldnt post vb .net question here.
My bad.
-Stein
"Jan Hyde" wrote:
> "Stein Lundbeck" 's wild
> thoughts were released on Tue, 25 Apr 2006 10:04:28 +0200
> bearing the following fruit:
>
> >Hi,
> >
> >I want to generate a number between 41 and 71.
> >To do this I use the following syntax:
>
> As far as I can tell that is not VB6 code, if your
> programming VB.Net then try a dotnet group.
>
> Although many commands have the same name between the two
> languages, they often work differently.
>
> J
>
> >Randomize()
> >CInt(Int((71 * Rnd()) + 41))
> >
> >
> >I wrote the following function just to test this:
> >
> >Private Sub TestRnd()
> > Randomize()
> >
> > Dim i As Int32 = 0
> >
> > Do While i < 50
> > Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
> >
> > i += 1
> > Loop
> > End Sub
> >
> >
> >These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
> >97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
> >
> >As you can see, there are many numbers that is not between 41 and 71. Even
> >numbers with 3 digits!
> >
> >So what I am doing wrong ?
> >
> >BR,
> >Stein
> >
>
>
> Jan Hyde (VB MVP)
>
> --
> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
>
> |
|
| Back to top |
|
 |
Gary Chang[MSFT]
Joined: 04 Oct 2007 Posts: 5
|
Posted: Tue Apr 25, 2006 2:14 pm Post subject: RE: Problems with the Rnd() function |
|
|
Hi Stein,
You can resolve the problem as the following 2 steps:
1. Generate a random number between 0 and 30---Int((31 * Rnd())
2. Plus 41 to the step1's result number---Int((31 * Rnd()) + 41.
Thanks!
Best regards,
Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights. |
|
| Back to top |
|
 |
Jan Hyde
Joined: 04 Oct 2007 Posts: 1211
|
Posted: Tue Apr 25, 2006 4:54 pm Post subject: Re: Problems with the Rnd() function |
|
|
Stein Lundbeck 's
wild thoughts were released on Tue, 25 Apr 2006 03:03:02
-0700 bearing the following fruit:
>Sorry Jan,
>
>I didnt know I couldnt post vb .net question here.
This is for your benefit more that anyone, by posting to the
approprate group you'll get more answers from people you
know the subject, rather than the odd poster here who might
know a little about your subject.
An excelent starting point for .Net quesions is
microsoft.public.vb.general.discussion
Regards
Jan
>My bad.
>
>-Stein
>
>"Jan Hyde" wrote:
>
>> "Stein Lundbeck" 's wild
>> thoughts were released on Tue, 25 Apr 2006 10:04:28 +0200
>> bearing the following fruit:
>>
>> >Hi,
>> >
>> >I want to generate a number between 41 and 71.
>> >To do this I use the following syntax:
>>
>> As far as I can tell that is not VB6 code, if your
>> programming VB.Net then try a dotnet group.
>>
>> Although many commands have the same name between the two
>> languages, they often work differently.
>>
>> J
>>
>> >Randomize()
>> >CInt(Int((71 * Rnd()) + 41))
>> >
>> >
>> >I wrote the following function just to test this:
>> >
>> >Private Sub TestRnd()
>> > Randomize()
>> >
>> > Dim i As Int32 = 0
>> >
>> > Do While i < 50
>> > Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
>> >
>> > i += 1
>> > Loop
>> > End Sub
>> >
>> >
>> >These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
>> >97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
>> >
>> >As you can see, there are many numbers that is not between 41 and 71. Even
>> >numbers with 3 digits!
>> >
>> >So what I am doing wrong ?
>> >
>> >BR,
>> >Stein
>> >
>>
>>
>> Jan Hyde (VB MVP)
>>
>> --
>> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
>>
>>
Jan Hyde (VB MVP)
--
Rich foods are like destiny. They, too, shape our ends. (Art. Moger) |
|
| Back to top |
|
 |
Al Reid
Joined: 04 Oct 2007 Posts: 230
|
Posted: Tue Apr 25, 2006 12:08 pm Post subject: Re: Problems with the Rnd() function |
|
|
"Jan Hyde" wrote in message @4ax.com...
> Stein Lundbeck 's
> wild thoughts were released on Tue, 25 Apr 2006 03:03:02
> -0700 bearing the following fruit:
>
> >Sorry Jan,
> >
> >I didnt know I couldnt post vb .net question here.
>
> This is for your benefit more that anyone, by posting to the
> approprate group you'll get more answers from people you
> know the subject, rather than the odd poster here who might
> know a little about your subject.
>
> An excelent starting point for .Net quesions is
>
> microsoft.public.vb.general.discussion
>
I suppose you meant to say "microsoft.public.dotnet.languages.vb" is an excellent starting point for vb.Net questions.
> Regards
>
> Jan
>
> >My bad.
> >
> >-Stein
> >
> >"Jan Hyde" wrote:
> >
> >> "Stein Lundbeck" 's wild
> >> thoughts were released on Tue, 25 Apr 2006 10:04:28 +0200
> >> bearing the following fruit:
> >>
> >> >Hi,
> >> >
> >> >I want to generate a number between 41 and 71.
> >> >To do this I use the following syntax:
> >>
> >> As far as I can tell that is not VB6 code, if your
> >> programming VB.Net then try a dotnet group.
> >>
> >> Although many commands have the same name between the two
> >> languages, they often work differently.
> >>
> >> J
> >>
> >> >Randomize()
> >> >CInt(Int((71 * Rnd()) + 41))
> >> >
> >> >
> >> >I wrote the following function just to test this:
> >> >
> >> >Private Sub TestRnd()
> >> > Randomize()
> >> >
> >> > Dim i As Int32 = 0
> >> >
> >> > Do While i < 50
> >> > Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
> >> >
> >> > i += 1
> >> > Loop
> >> > End Sub
> >> >
> >> >
> >> >These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
> >> >97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
> >> >
> >> >As you can see, there are many numbers that is not between 41 and 71. Even
> >> >numbers with 3 digits!
> >> >
> >> >So what I am doing wrong ?
> >> >
> >> >BR,
> >> >Stein
> >> >
> >>
> >>
> >> Jan Hyde (VB MVP)
> >>
> >> --
> >> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
> >>
> >>
>
>
> Jan Hyde (VB MVP)
>
> --
> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
> |
|
| Back to top |
|
 |
Bob Butler
Joined: 04 Oct 2007 Posts: 1325
|
Posted: Tue Apr 25, 2006 10:19 am Post subject: Re: Problems with the Rnd() function |
|
|
"Jan Hyde" wrote in message@4ax.com
> An excelent starting point for .Net quesions is
>
> microsoft.public.vb.general.discussion
I agree, except that I'd replace "excellent" with "completely inappropriate"
--
Reply to the group so all can participate
VB.Net: "Fool me once..." |
|
| Back to top |
|
 |
argusy
Joined: 04 Oct 2007 Posts: 145
|
Posted: Wed Apr 26, 2006 3:39 am Post subject: Re: Problems with the Rnd() function |
|
|
and for all of the nine replies to the op, either chastising him for being in a
group dedicated to classic VB (which in this case is a non-event), or providing
an answer, no one gave you what you wanted!!
If you want numbers from 41 to 71 then the base number is NOT THIRTY
IT IS FORTY ONE !!!
so to get the right range of numbers (41 to 71)
the code should be
Int( 41 * Rnd() + 30)
note normal rules of multiplication, followed by addition need apply
so brackets are required to enclose INT( ), and the function Rnd()
Argusy
Stein Lundbeck wrote:
> Hi,
>
> I want to generate a number between 41 and 71.
> To do this I use the following syntax:
>
> Randomize()
> CInt(Int((71 * Rnd()) + 41))
>
>
> I wrote the following function just to test this:
>
> Private Sub TestRnd()
> Randomize()
>
> Dim i As Int32 = 0
>
> Do While i < 50
> Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
>
> i += 1
> Loop
> End Sub
>
>
> These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
> 97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
>
> As you can see, there are many numbers that is not between 41 and 71. Even
> numbers with 3 digits!
>
> So what I am doing wrong ?
>
> BR,
> Stein
>
> |
|
| Back to top |
|
 |
Rick Rothstein
Joined: 04 Oct 2007 Posts: 1589
|
Posted: Tue Apr 25, 2006 2:23 pm Post subject: Re: Problems with the Rnd() function |
|
|
> so to get the right range of numbers (41 to 71)
>
> the code should be
>
> Int( 41 * Rnd() + 30)
You have that backwards. The general formula is this...
Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)
and substituting the given range produces this...
Int((71 - 41 + 1) * Rnd() + 41)
which reduces to...
Int(31 * Rnd() + 41)
Rick |
|
| Back to top |
|
 |
Jan Hyde
Joined: 04 Oct 2007 Posts: 1211
|
Posted: Tue Apr 25, 2006 7:40 pm Post subject: Re: Problems with the Rnd() function |
|
|
"Al Reid" 's wild thoughts were
released on Tue, 25 Apr 2006 08:08:29 -0400 bearing the
following fruit:
>"Jan Hyde" wrote in message @4ax.com...
>> Stein Lundbeck 's
>> wild thoughts were released on Tue, 25 Apr 2006 03:03:02
>> -0700 bearing the following fruit:
>>
>> >Sorry Jan,
>> >
>> >I didnt know I couldnt post vb .net question here.
>>
>> This is for your benefit more that anyone, by posting to the
>> approprate group you'll get more answers from people you
>> know the subject, rather than the odd poster here who might
>> know a little about your subject.
>>
>> An excelent starting point for .Net quesions is
>>
>> microsoft.public.vb.general.discussion
>>
>
>I suppose you meant to say "microsoft.public.dotnet.languages.vb" is an excellent starting point for vb.Net questions.
Cut & paste bites me in the ass again
J
>
>> Regards
>>
>> Jan
>>
>> >My bad.
>> >
>> >-Stein
>> >
>> >"Jan Hyde" wrote:
>> >
>> >> "Stein Lundbeck" 's wild
>> >> thoughts were released on Tue, 25 Apr 2006 10:04:28 +0200
>> >> bearing the following fruit:
>> >>
>> >> >Hi,
>> >> >
>> >> >I want to generate a number between 41 and 71.
>> >> >To do this I use the following syntax:
>> >>
>> >> As far as I can tell that is not VB6 code, if your
>> >> programming VB.Net then try a dotnet group.
>> >>
>> >> Although many commands have the same name between the two
>> >> languages, they often work differently.
>> >>
>> >> J
>> >>
>> >> >Randomize()
>> >> >CInt(Int((71 * Rnd()) + 41))
>> >> >
>> >> >
>> >> >I wrote the following function just to test this:
>> >> >
>> >> >Private Sub TestRnd()
>> >> > Randomize()
>> >> >
>> >> > Dim i As Int32 = 0
>> >> >
>> >> > Do While i < 50
>> >> > Console.WriteLine(CInt(Int((71 * Rnd()) + 41)))
>> >> >
>> >> > i += 1
>> >> > Loop
>> >> > End Sub
>> >> >
>> >> >
>> >> >These are some of the numbers that was generated: 105, 77, 98, 48, 69, 99,
>> >> >97, 87, 71, 108, 110, 84, 62, 63, 57, 50, 50, 102, 90 ,85
>> >> >
>> >> >As you can see, there are many numbers that is not between 41 and 71. Even
>> >> >numbers with 3 digits!
>> >> >
>> >> >So what I am doing wrong ?
>> >> >
>> >> >BR,
>> >> >Stein
>> >> >
>> >>
>> >>
>> >> Jan Hyde (VB MVP)
>> >>
>> >> --
>> >> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
>> >>
>> >>
>>
>>
>> Jan Hyde (VB MVP)
>>
>> --
>> Rich foods are like destiny. They, too, shape our ends. (Art. Moger)
>>
>
Jan Hyde (VB MVP)
--
Rich foods are like destiny. They, too, shape our ends. (Art. Moger) |
|
| Back to top |
|
 |
Jan Hyde
Joined: 04 Oct 2007 Posts: 1211
|
Posted: Tue Apr 25, 2006 7:41 pm Post subject: Re: Problems with the Rnd() function |
|
|
"Bob Butler" 's wild thoughts were
released on Tue, 25 Apr 2006 06:19:40 -0700 bearing the
following fruit:
>"Jan Hyde" wrote in message
>@4ax.com
>> An excelent starting point for .Net quesions is
>>
>> microsoft.public.vb.general.discussion
>
>I agree, except that I'd replace "excellent" with "completely inappropriate"
>
I'm wearing the dunces cap as I type.
Jan Hyde (VB MVP)
--
Rich foods are like destiny. They, too, shape our ends. (Art. Moger) |
|
| Back to top |
|
 |
argusy
Joined: 04 Oct 2007 Posts: 145
|
Posted: Wed Apr 26, 2006 4:16 am Post subject: Re: Problems with the Rnd() function |
|
|
aaarrrrgggghhhh!!!!
Midnight again
I should go to bed
I started off thinking you're wrong, wrote it out and ended up convincing myself
I was wrong.
I wuz wrong!!
(mumble, mumble .. get a life Graham)
Argusy
Rick Rothstein wrote:
>>so to get the right range of numbers (41 to 71)
>>
>>the code should be
>>
>>Int( 41 * Rnd() + 30)
>
>
> You have that backwards. The general formula is this...
>
> Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)
>
> and substituting the given range produces this...
>
> Int((71 - 41 + 1) * Rnd() + 41)
>
> which reduces to...
>
> Int(31 * Rnd() + 41)
>
> Rick
>
>
|
|
| Back to top |
|
 |
|
|
| Related Topics: | dateformat problems is there a function to get the format of the system date? Can you change it in the code (the alternative : how do I set a date to the first of a certain month without having to think about the for example: Receiveddate
Problems with collections Hi there, I have a problem. I am unable to access a collection item by its key. I have always tried to do this, but never could, and now I REALLY REALLY need to be able to do it. Here is my code, please help: 'Fields is a collection item
Problems with If..Then..Else Statement My Code looks like the following: If LocationID = 0 Then to execute command") Else Dim SqlStr As String Dim odr SqlStr = "DELETE FROM Locations WHERE LocationID = " & LocationID = OleDbCom
Guestbook problems On my homepage I have a guestbook. Just before christmas someone wrote a message in my guestbook. My guestbook pretty much look like this page when you post a new message. So what happend is that the message added in my guestbook ended with ¤¤¤. Normally
Put Variables giving problems I don't know how to "link" my posts to another newsgroup, but - this Syntax one seems more like where the original post is supposed to be. I'm sorry for crossposting, but noone's answered the others anyway. ;) Assume Word 2000 VBA. myDataType Name as str |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|