Click here to Skip to main content
15,568,971 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want mathematical equation or function to get number between two number .. for example the two number is 5 and 10 .. when i send 5 will make process and returned number for example 7 this result will not be repeated .. and when i send 8 . will return for example 10

What I have tried:

this is example
void main()
    int from1 =5,to=10;
    for(int a=from1 ;a<=to;a++)
        int result=gen(from1 ,to,a);

int gen(int min , int max,int value)
    //The result is generated by a calculation based on three values, and this
    //result will not be repeated
    //The result is between the smallest value and the largest value
    return result;

I used many methods but all of them store in memory and I don't want it. for example

void not_good()
    var ran = Enumerable.Range(from, to).OrderBy(x => Guid.NewGuid());
    foreach (int i in ran)
    List<int> listNumbers = new List<int>();
    int number;
    for (int i = from; i < to; i++)
      do {
         number = rand.Next(from, to);
      } while (listNumbers.Contains(number));

and i used Random function not solve me
Updated 8-Feb-21 9:08am
gggustafson 22-Mar-20 13:36pm    
And how would you maintain knowledge of the earlier generated values without using some form of memory storage?
Luc Pattyn 8-Feb-21 22:00pm    
Your specs are incomplete:
- if your program is ran twice, should it generate the same series of results, or not?
- if your program would have a second for loop, identical to the one already present, should the second loop generate the same series of results, or not?

And is there some upper bound to the number of values involved, i.e. what is max(to-from) ?


i want mathematical equation or function to get number between two number

Short answer is "it does not exist". Both requests are mutually exclusive.
The closest thing is a "shuffle" algorithm, just like you shuffle a deck of playing cards and then you draw 1 by 1.
Fisher–Yates shuffle - Wikipedia[^]
Shuffle a given array using Fisher–Yates shuffle Algorithm - GeeksforGeeks[^]
Share this answer
Random numbers are just that: random. And that means that the next number returned by a generator does not rely on previous results at all: it is as likely to generate any number in the sequence each time you try to get a new random number.
And there is another problem: if you retrieve numbers in a range x to y - say 5 to 9 inclusive - what happens when you try to return the (n = (y - x) + 2)th number? You have used all of the values so far 7 9 6 5 8 ? ...

The only way to do it is to store in memory the numbers to fetch from and remove values from the collection when you use them. (or the numbers you have fetched, but that needs to non-deterministic "next value" fetch times)
Share this answer
I searched and searched the internet and did not like the solutions I was seeing. Yes, I saw the Fisher-Yates Shuffle but its like greek to me. I wanted a simpler, easy to follow solution that was more how it would happen in the real world (ie. 14 ping pong balls in a jar that you shake up and you pull out 3 without looking. each one would be a different number). What I came up with is this.

Visual Basic baffles me a bit so maybe someone could come up with a more streamlined solution idk. The variable names obviously could be shortened.

' Converted the numbers to a text string so that each number could be 2 digits
'     if the numbers were in the array as "1,2,3,4,5,6,7,8,9,10,11,12,13,14"
'     then when the random number came up 1 the replace function would only
'     strip the 1 from numbers 10,11,12,13 & 14 and would leave the number 1
' Added 00 as a number that will never be selected so the first number we
'     do want to pick (ie. 01) will have a comma in front of it
'     if the array started with 01,02,03 etc.) then the replace function
'     would miss 01 as it does not have a comma in front of it
' Added +1 to the random number so that 00 is never picked

Dim i1, i2, i3
dim myNumbersAsText1, myNumbersAsText2, myNumbersAsText3
Dim myarray1, myarray2, myarray3
dim mypick1, mypick2, mypick3

	myNumbersAsText1 = "00,01,02,03,04,05,06,07,08,09,10,11,12,13,14"
	myarray1 = split(myNumbersAsText1,",")
	i1 = int(rnd*14)+1
	mypick1 = CInt (myarray1(i1))

	myNumbersAsText2 = Replace(myNumbersAsText1, "," & myarray1(i1), "")
	myarray2 = split(myNumbersAsText2,",")
	i2 = int(rnd*13)+1
	mypick2 = CInt (myarray2(i2))

	myNumbersAsText3 = Replace(myNumbersAsText2, "," & myarray2(i2), "")
	myarray3 = split(myNumbersAsText3,",")
	i3 = int(rnd*12)+1
	mypick3 = CInt (myarray3(i3))

'******* Everything below can be deleted **********
'******* it is just to show the results  **********

'turns an array into a string delimited by commas
Dim strText1, strText2, strText3
strText1 = join(myarray1,",")
strText2 = join(myarray2,",")
strText3 = join(myarray3,",")

msgbox 	strText1 & vbCRLF & i1 & " = " & myarray1(i1) & vbCRLF &_
	strText2 & vbCRLF & i2 & " = " & myarray2(i2) & vbCRLF &_
	strText3 & vbCRLF & i3 & " = " & myarray3(i3) & vbCRLF & vbCRLF & _
	"Picked Numbers = " & mypick1 & ", " & mypick2 & ", " & mypick3
Share this answer
CHill60 9-Feb-21 14:03pm    
"The variable names obviously could be shortened" - why bother - it doesn't streamline the code in any way. I think I'm more puzzled as to why you have posted a VBA solution to a C# question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900