Producing random and different numbers

# Thread: Producing random and different numbers

1. Junior Member
Join Date
Dec 1969
Posts
1

## Producing random and different numbers

I have to produce, for example 8 numbers smaller than 10, with randomize (rnd) function into an array of 8 elements. But the problem that i have to solve is that sometimes same numbers are produced. In the project that i work on, this is not wanted. I am working on this problem nearly for two days but havent solved it yet. Would anyone help me about this?

2. Senior Member
Join Date
Dec 1969
Posts
1,407

## RE: Producing random and different numbers

can you show some of your code?<BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Sounds like homework...

...for a beginning class in programming. Am I right?<BR><BR>So no code, but I&#039;ll point you in the right direction.<BR><BR>THINK about it: <BR><BR>You have an array with 8 slots in it, yes?<BR><BR>You pick some random number and put it in the first slot, yes?<BR><BR>Then you pick another random number. How do you know it doesn&#039;t match the first one??? Simple answer: You go check the first slot. If it matches, start over on picking the second number. If it doesn&#039;t match, put the number in the second slot.<BR><BR>Then you pick another random number. How do you know it doesn&#039;t match the first *OR* second one??? Simple answer: You go check the first slot. If it matches, start over on picking the third number. You go check the second slot. If it matches you start over on picking the third number. If it doesn&#039;t match, put the number in the third slot.<BR><BR>... repeat for 4th, 5th, 6th, 7th...<BR><BR>Then you pick the 8th random number. How do you know it doesn&#039;t match the first through seventh ones??? Simple answer: You go check the first through seventh slots. If it matches any of them, start over on picking the 8th number. If it doesn&#039;t match any of them, put the number in the 8th slot.<BR><BR>Can you see that, now?<BR><BR><BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
1,407

## RE: Sounds like homework...

dim i, x<BR>dim thearray[8]<BR><BR>for i = 0 to 7<BR>thearray[i]=rnd() &#060;--i don&#039;t know how to get your random number under 10 off the top of my head<BR>if i &#062; 0 then<BR>for x = 0 to i<BR>if i&#060;&#062;x then<BR>do while thearray[i]=thearray[x]<BR>thearray[i]=rnd()<BR>loop<BR>end if<BR>next<BR>end if<BR>next

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Nope. Not very close.

Did you actually try it?<BR><BR>And to get a number from 1 to 10 you just use: <BR> 1 + Int( 10 * Rnd() )<BR><BR>Don&#039;t forget to use RANDOMIZE *once* per page.<BR><BR>Now go try your code. It won&#039;t work.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
1,407

## figures

Expected end of statement<BR>/Project8/scripts/random.asp, line 4, column 12<BR>dim thearray[8]

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## That's just because...

...you got your VBScript and JavaScript mixed up.<BR><BR>VBScript *NEVER* uses square brackets [...] in any coding.<BR><BR>But even if you change all the [...] to (...) it&#039;s still not going to work. Honest.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
1,407

## RE: figures

hey, it worked, bill. all i had to do was go in and change out the brackets with parenthesis.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
1,407

## RE: figures

oh, ****. no, it doesn&#039;t work.<BR><BR>i just produced this output:<BR><BR>8,6,3,4,1,9,5,8

10. Senior Member
Join Date
Dec 1969
Posts
1,407

## what the hell

and i can&#039;t get it to refresh either. i hit refresh, i closed down the browser and revisited the page........no good. why?<BR><BR>tell me what i&#039;m doing wrong......c&#039;mon, don&#039;t drag this out on me. LOL.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•